JQuery AJAX Success Method explanation

I am trying to use this jQuery script and it confuses 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 }); } 

Bit embarrassed is a 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 does not look like this:

 success : function(msg) {//On Successfull service call ServiceSucceeded(msg); } 

The object that we seem to be passing.

Questions:

1) What does function(msg){ServiceSucceeded(msg)} ?

2) What is "msg" in this context?

3) How, in fact, I wanted to know how to structure the sugnature method for success?

+8
javascript jquery ajax
source share
7 answers

A perfectly reasonable question. :-) In JavaScript, you do not have to call a function with as many arguments as it defines, and you do not need to define as many arguments as you can call. This can be confusing if you are used to more limited environments. :-)

Response Features:

1) What does the function (msg) {ServiceSucceeded (msg)} mean?

Defines a function (anonymous) that takes one named argument ( msg ) and calls ServiceSucceded , passing in that argument. jQuery will call a function with three arguments defined in the jQuery documentation for the success function, but in this particular success function, only the first one ( data ) is used. Read more about the named functions and anonymous functions here .

2) What is "msg" in this context?

The first argument to the function. jQuery docs call this first data argument, but you can call it whatever you want.

3) How, in fact, I wanted to know how to structure the sugnature method for success?

You did the right thing, this is in the jQuery documentation.

This thing about function arguments can be confusing, so let's do some examples:

 function foo(arg) { alert(arg); } 

It is very clear that I am defining a function called foo that takes one named argument, arg . And thus:

 foo("Hi there"); // alerts "Hi there" 

But I can also do this:

 foo(); // alerts "undefined" 

There I did not give any arguments for foo , so inside foo , arg is undefined.

I can also do this:

 foo("Hi there", "again"); // alerts "Hi there" 

I call foo with two arguments, but foo uses only one of them.

I could define foo to use as many arguments as you go:

 function foo() { var index; for (index = 0; index < arguments.length; ++index) { alert(arguments[index]); } } 

arguments is an automatic thing that all functions have, which is a pseudo-array (this is not really an Array ) of the actual arguments with which the function was called. So:

 foo("Hi there", "again"); // alerts "Hi there", and then alerts "again" 

You can even mix named and unnamed arguments:

 function foo(arg) { var index; alert(arg); for (index = 1; index < arguments.length; ++index) { alert("[" + arguments[index] + "]"); } } 

So now

 foo("Hi there", "again"); // alerts "Hi there" and then alerts "[again]" 

Pay attention to the [] around the second warning, because I started the loop with index 1 , not zero.

arguments and named args are related:

 function foo(arg) { alert("arg = " + arg); alert("arguments[0] = " + arguments[0]); arg = "Updated"; alert("arg = " + arg); alert("arguments[0] = " + arguments[0]); } 

If I do foo("Hi"); This shows the following warnings:

  arg = Hi
 arguments [0] = Hi
 arg = Updated
 arguments [0] = Updated 

(There is a different way if you update arguments[0] .)

+15
source share

The function is passed to 3 parameters: data , status and jqXHR . data is what is returned from an AJAX call, status is the HTTP status code (I think), and jqXHR is an XHR object wrapped by jQuery.

In this script, they are only interested in the data parameter, not the other two.

Thus, using success: function(msg) , they only get the data parameter. The other two are sent, but ignored.

ServiceSucceeded is just a function called with the data parameter sent to it.

success: ServiceSucceeded might also work here.

+6
source share
  • This means that the success handler calls ServiceSucceeded to respond to the request.
  • msg contains the response from the request. msg displayed in data in the jQuery documentation.
  • You need to look into the jQuery documentation for a signature.
+2
source share

1) This function is called if the AJAX request is successful, that is, the success status code is returned using the linked server.

2) I would suggest that "msg" is the data returned from the server. The remaining two arguments are not given and therefore are not used.

3) Use the jQuery documentation and play until you get what you want.

+1
source share
  • This is an anonymous feature.
    It is like a normal function, but without a name.

  • msg is the first parameter to the function.

  • After reading the documentation.

+1
source share

Despite the fact that the success function is defined as the adoption of three parameters (in accordance with the documentation quoted by you), these three parameters are not required: Javascript is very forgiving about this; if you skip the parameter from the function call, it just gets the value underfined , so until you try to use it, JS will not cause any errors.

The code you provided gives only one parameter - msg - but in JS this is completely true; it just means that msg will be the data parameter defined in the docs, and textStatus and jqXHR will be undefined.

This is great if in your success function you actually do not want to use any of these parameters. If you want to use them, then pass them, but if not, you should discard them. You write a success function, so you can decide which of the three parameters to use.

+1
source share

jquery Ajax is a way to communicate with the server (PHP, ASP, whatever). Suppose you are using PHP. function "callService ()" sends a request to "varUrl" (validation.php, ie) and receives (or POST β†’ varType) the contents (varContentType β†’ valdation.php? id = 1231 & what = soemthing). The purpose of this is to obtain server-side data without reloading the page. If you want validation.php to be echo html , then the data type in the Ajax function should be "html". See Jquery.com for more information on data type.

The success parameter is the function handler for the server response. Success is called if you get a response from the server that matches the requested data type (html, json, text, whatever). In this particular case, if the server answers correctly, the "ServiceSucceeded" function is called with the "msg" attribute, which is the response of the server that you requested.

+1
source share

All Articles