Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) jQuery and AJAX The Office Status Project Revisited

Where did the function (response) came from?

In ajax, we have object property with name of responseText. But dave is just passing response as a parameter. How does he know?

3 Answers

huckleberry
huckleberry
14,636 Points

I'll elaborate on this a bit and maybe clear up any confusion because as I see it the main question is "why does "response" work as the parameter? Aren't we supposed to use responseText? How does it know what the responseText is?"

Simple. jQuery is handling all of that for you and this method's 2nd parameter, the callback function...

$.getJSON(url, callback(parameter){
    stuffToDo;
});

...has one parameter.

That parameter, upon a successful response from the server (i.e. the request has been made successfully and the server sent back the JSON file), will automatically have the value of the responseText property passed into it.

You can name that parameter that Dave named response whatever you like... Dave was simply naming it response to drive the point home that this parameter will indeed contain the data that's inside of the XHR Object's responseText property.

I hope that cleared it up for anyone that may still have been confused. :)

Cheers,

Huck - :sunglasses:

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

Yes, after you make your request and a response is sent back, the callback function automatically executes.

$.getJSON(url, callback);

In his example he uses an anonymous function

$.getJSON(url, function(response){
  //do stuff
});

You could call the parameter anything. It doesn't have to be "response". A lot of times you will see "data" used

$.getJSON(url, function(data){
  //do stuff
});

You could also declare the function separately and then use it in your ajax call

function handleDataFromServer(response){
  //do stuff
}

$.getJSON(url, handleDataFromServer);

But basically you can name that parameter anything you want. The only thing that will change is how you use it in the function body

$.getJSON(url, function(response){
  alert( response.name );
});

$.getJSON(url, function(foo){
  alert( foo.name );
});
Michael Hulet
Michael Hulet
47,912 Points

The onreadystatechange property on any XMLHttpRequest object is to contain a function that handles the response. That means that the function has to have a way of figuring out what the response is. JavaScript handles this by automatically handing that function an object containing the response, which is passed in as a parameter to that function by JavaScript. The name "response" is just a convention - you could name it anything you want