Reputation: 177
I'm on my way to learning more about javascript and it could get really confusing at most times.
So we have this in jQuery:
$.getJSON('getDate.php', function(response, status, xhr) { ... });
And this:
$('a').click(function(event) { ... });
Based on the code above and in terms of javascript definitions, please consider these arguments(response, status, xhr
& event
) and the questions about them as listed below:
I'm a newbie programmer and I'm not sure if I asked the right questions.
Thanks.
Upvotes: 3
Views: 216
Reputation: 7253
$.getJson is jquery function for requesting json object form the server(AJAX). so here -Response: This is what your server returns based on your request. -status: This tells whether the communication between the client and the server was successful and was able to send the response. -xhr: its the calback function called when the request completes/terminates.
as in case of the $('a').click() -Event: tells the browser that a click even has been raised and fired.
Upvotes: 0
Reputation: 46477
So going off of your click example....
$('a').click(function(event) { ... });
Here, you are defining what should happen when an anchor is clicked. You are defining the function that defines this behavior, namely function(event) { ... }
. This is known as the "callback" function. So once jQuery is done doing its work, it executes the callback function, the function you have defined.
In addition, event
is a parameter that jQuery passes in to your callback function. Specifically, event
is an event Object defined by jQuery. You may or may not need this, but in the case that you want to do something with the event Object, you have that available.
I hope you can extrapolate from this explanation how the $.getJSON()
stuff works. The other answers do a good job of explaining that, particularly Purmou's answer and gideon's answer.
Upvotes: 0
Reputation: 17061
response
holds the data that was retrieved from the page requested using the $.get()
function. In that function, you can use response
to reference said data.status
returns a string that determines the status of the request. It should always be success
, because the callback will not be fired unless the request was successful.xhr
is jQuery's XHR (XMLHttpRequest) object returned by the success function. "It implements the Promise interface, giving it all the properties, methods, and behavior of a Promise." (from jquery.com)event
is jQuery's event object.Before asking a question like this, however, it's vital that you explore the docs of the library you're asking about. Your questions would have been answered had you done so in the first place.
Upvotes: 1
Reputation: 19465
So your question is a little broad, but I will try to explain how in javascript functions are fist class objects. Basically you can pass and use a function just like a variable:
The say_something function here, expects a function passed to it :
function say_something(fn)
{
if(typeof(fn) == "function")//if the param is a function
{
fn("hello");//run that function and pass in hello
}
}
say_something(
function(msg) { //I'm passing this function as a param to say_something
alert(msg);
}
);
Now can you relate this to $.getJSON?
The $.getJSON
function is just like say_something
. It expects a function for the second parameter, if that is a function is calls it and passes response
, status
and xhr
as parameters to that function.
The getJSON
calls .get
which calls .ajax()
, see:
http://james.padolsey.com/jquery/#v=1.6.2&fn=jQuery.getJSON
http://james.padolsey.com/jquery/#v=1.6.2&fn=jQuery.get
http://james.padolsey.com/jquery/#v=1.6.2&fn=jQuery.ajax
See how eventually ajax()
accepts an options{}
hashset which contains the callback function, and eventually calls it like this callback.call(...
Upvotes: 2
Reputation: 1157
In order to know more about "$.getJSON" or the arguments "response, status, xhr", you should firstly know AJAX, and "$.getJSON" is just an encapsulation of AJAX, "response, status, xhr" are gave by AJAX's callback. To learn more: AJAX
and it is highly recommended for newbie to use AJAX directly instead of some tools (jQuery or dojo) at first.
Upvotes: 0
Reputation: 54806
They are parameters that will be provided to your function at the time it is invoked. Note that the names are not significant, but the ordering is.
They come from the calling code. In this case, that would be jQuery. It will set up the appropriate values for these parameters and pass them to your function(s) at invocation time.
When you need/want to. Check the jQuery documentation for details on exactly what information each parameter contains. That will give you a better idea on when you might want to do something with a given parameter.
Just refer to them by name in your function code (i.e. the stuff between {
and }
). They work the same as any other variable in JavaScript. Refer to the jQuery documentation for a list of fields that are available on each different parameter.
Upvotes: 4
Reputation: 262494
getJSON
and click
define a signature for the callback functions that you give them.
These are the parameters that the callback will be given, when it is invoked (after the JSON has loaded, or a click has happened).
Check the jQuery documentation for details:
Upvotes: 0
Reputation: 707258
In this case with callbacks, the call of the function defines what arguments will be passed to the function when it is called.
For $.getJSON()
, you can read how those arguments are defined here.
response
is the data returned from the JSON call
status
is the status of the http call
xhr
is a superset of the XMLHTTPRequest object used in the ajax call.
For the jQuery click()
function, it sends the callback the event object from the actual click event.
Upvotes: 0