Skizit
Skizit

Reputation: 44862

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.

Upvotes: 0

Views: 280

Answers (3)

Oscar Jara
Oscar Jara

Reputation: 14187

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.

Upvotes: 0

clime
clime

Reputation: 8885

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.

Upvotes: 0

James M
James M

Reputation: 16718

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

      response (data);
});

Upvotes: 3

Related Questions