Im trying to use this jQuery script and this is confusing me:
function CallService()
{
$.ajax({
type : varType, //GET or POST or PUT or DELETE verb
url : varUrl, // Location of the service
data : varData, //Data sent to server
contentType : varContentType, // content type sent to server
dataType : varDataType, //Expected data format from server
processdata : varProcessData, //True or False
success : function(msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
The bit im confused about is the sucess object. The jQuery documentation says:
success(data, textStatus, jqXHR)Function, Array
A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.
But this method signature looks nothing like the:
success : function(msg) {//On Successfull service call
ServiceSucceeded(msg);
}
Object that we seem to be passing in.
Questions:
1) What does function(msg){ServiceSucceeded(msg)} mean?
2) What is ‘msg’ in this context?
3) How on earth am I meant to know how to structure the method sugnature for sucess?
Perfectly reasonable question. 🙂 In JavaScript, you don’t necessarily have to call a function with as many args as it defines, and you don’t have to define as many args as you may get called with. Which can be confusing if you’re used to more constrained environments. 🙂
Answering specifics:
It defines a function (an anonymous one) that accepts one named argument (
msg) and callsServiceSuccededpassing in that arg. jQuery will call the function with the three arguments defined by the jQuery documentation for thesuccessfunction, but this particularsuccessfunction is only using the first of those (data). More about named functions vs. anonymous functions here.The first argument to the function. jQuery’s docs call this first argument
data, but you can call it whatever you like.You did the right thing, it’s in the jQuery documentation.
This thing about function arguments can be confusing, so let’s do some examples:
That’s perfectly clear, I’m defining a function called
foothat takes a single named argument,arg. And thus:But I can also do this:
There, I didn’t give any arguments for
foo, and so withinfoo,argis undefined.I can also do this:
I’m calling
foowith two arguments, butfooonly makes use of one of them.I could define
footo use as many arguments as you pass in:argumentsis an automatic thing all functions have, which is a pseudo-array (it’s not really anArray) of the actual arguments the function was called with. And so:You can even mix named and unnamed arguments:
So now
Note the
[]around the second alert, because I started looping with index1rather than zero.argumentsand named args are connected:If I do
foo("Hi");, that shows these alerts:(It goes the other way, too, if you update
arguments[0].)