简体   繁体   中英

Equivalent Callback function for “$.getJSON(”myurl“,response);”?

$.getJSON( "myurl", response);

What would the equivalent callback function version of the above look like? I've tried...

$.getJSON("myurl",function(data){
      //manipulate data
      return data;
});

but it doesn't appear to produce the same result.

I'm working with JQuerys AutoSuggest library and attempting to manipulate the response I receive back from my server before sending it on.

$.getJSON ("myurl", function (data)
{
      // manipulate data

      response (data);
});

In the first case, you pass data that are sent to the server. In the second case, you pass a success callback. There is no way of making an "equivalent callback version" to the first usage. See jQuery.getJSON .

The callback will be the same but you can manipulate data in different ways, for example:

As you specified two types, look case 1 and 2

case 1:

//start and receive callback
function send()
{ 
  var v = $("element").attr("value");
  $.getJSON("page.php",{ v:v }, responseData); 
  return false;
}

//manipulate callback data
function responseData(data)
{
  $("#results").html("Name: " + data.name + "<br/>" + "Lastname: " + data.lastname);
}

case 2:

$.getJSON ("page.php", function (data)
{
   $("#results").html("Name: " + data.name + "<br/>" + "Lastname: " + data.lastname);
});

Hope this helps.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM