jason white
jason white

Reputation: 711

The following JQuery code performing Ajax call

The data format returning from AJAX call is as followed

{
  "items" : {
    "Phones" : "PhoneSelect",
    "NoteBooks" : "notebookSelect",
    "Tablets" : ""
  },
  "title" : "What would you like to purchase?",
  "defaultText" : "Choose a product category"
}

The AJAX call

function fetchSelect (val) {
  $.getJSON('ajax.php', {key:val}, function(r) {
  $.each(r.items, function (k, v) {

According to the jQuery spec of .getJSON

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});
  1. My question is {key:val} the data returning from the call and then what r inside function(r)?

  2. I see the function pass in val , but what's key value for the {key:val}

Upvotes: -1

Views: 43

Answers (3)

Akash Yadav
Akash Yadav

Reputation: 2421

When you are calling the $.getJSON, it take three parameters 1. Url 2. INut Data 3. The callback function. So that answer to your question is that when the ajax call is complete jQuery will call the function passed in lieu of function(R) where R is the response from Server for the ajax call.

Upvotes: 0

Lix
Lix

Reputation: 47956

The {key:val} is actually what is being sent in the AJAX call to the server.

The r parameter is the data that is returned from the AJAX call. The data is returned as a parameter into your callback function.

Taking a look at the function signature in the documentation we can see what each parameter is supposed to be -

jQuery.getJSON( url [, data] [, success(data, textStatus, jqXHR)] )

  • url - A string containing the URL to which the request is sent.
  • data - A map or string that is sent to the server with the request.
  • success(data,textStatus, jqXHR) - A callback function that is executed if the request succeeds.

Your r parameter is the data being returned to the success callback. In your case, it is the JSON object.

Upvotes: 2

Sushanth --
Sushanth --

Reputation: 55740

{key:val} // The data sent to the server

r contains your callback result

r.items.Phones  // PhoneSelect
r.items.NoteBooks // notebookSelect

Upvotes: 1

Related Questions