[Solved] How to pass the jquery’s ajax response data to outer function? [duplicate]


You really should structure your program quite a bit better, and it would be easier if you could put your complete example up on jsfiddle.net.

But a quick solution in your current code may be to make “document” a global variable. Beware that document already is a global variable in the BOM (Browser Object Model), so use something else like say “docs”.

<script>
// Global variable
var docs;

jquery(document).ready(function(){
  jquery(#button).click(function{
    showData();  
  });
});

function showData(){
  var total  = "";
  var final = "";

  jquery.ajax({
    url : "AJAX_POST_URL",
    type: "POST",
    data : input,
    dataType: "json",
    success: function(jsondata)
    {
      //data - response from server
      docs = jsondata.data.results;  //am getting array of objects

      for(int i=0; i<document.length; i++) {
        var tr = "<tr>";
        var td = "<td>" + "Title : " +
        docs[i].title + "Url :" + "<a href="">" +  docs[i].url + </a> + "</td></tr>";
        final = tr + td;
        total = total + final;
      }

      $("#docTable").append(total) ;
    },

    error: function (jqXHR, textStatus, errorThrown)
    {
      alert("error");
    }
  });
}
</script>


<script>
function download(){    
  //inside this function i need to get the value of document[i].url

  // You have access to docs through the global variable
  for(var i=0;i < docs.length;i++) {
    console.log(docs[i].url)
  }
}
</script> 

13

solved How to pass the jquery’s ajax response data to outer function? [duplicate]