admin管理员组

文章数量:1326447

I am trying to find out what parameters I can pass to a jQuery ajax call.

What I am used to is writing something like:

$.ajax({
....
success: function(response) {
// put callback here
}
....
});

So here is my question:

Obviously the "response" variable I put in the success function just takes back whatever the server sends back from the ajax call. Is there any possible way to send multiple variables back? Something like:

...
success: function(response,httpStatus,whateverElse) {
}
...

Or, is there some other way to get the http response codes?

Thanks to whoever can answer this!

I am trying to find out what parameters I can pass to a jQuery ajax call.

What I am used to is writing something like:

$.ajax({
....
success: function(response) {
// put callback here
}
....
});

So here is my question:

Obviously the "response" variable I put in the success function just takes back whatever the server sends back from the ajax call. Is there any possible way to send multiple variables back? Something like:

...
success: function(response,httpStatus,whateverElse) {
}
...

Or, is there some other way to get the http response codes?

Thanks to whoever can answer this!

Share Improve this question asked Jan 30, 2015 at 1:38 Walker FarrowWalker Farrow 3,9249 gold badges36 silver badges56 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

You can get the response's status code on the success' third parameter or plete's first parameter, something like this:

$.ajax({
  success: function(data, textStatus, xhr) {
    console.log(xhr.status);
  },
  plete: function(xhr, textStatus) {
    console.log(xhr.status);
  } 
});

Further to @Kokizzu you can check the jQuery API site to see what parameters are passed to the other functions http://api.jquery./jquery.ajax/.

Also another way that I find handy to work out what parameters are being passed when there are no docs available is:

success: function() {
 console.log(arguments);
}

That will log to the console all of the arguments that were passed to that function when it was called.

You can also have the server send back json json_encode in php:

Php:

$array['status'] = 0;
$array['foo'] = 'bar';

json_encode($array);

Ajax:

$.ajax({
    ...
    success: function (data) {
        console.log(data);
    }
});

Then obviously you could have your callback handle those variables.

$.ajax({
  success: function(data, status, xhttp) {
    console.log(status + ": " + data);
  },
  error: function(data, status, xhttp) {
    console.log(status + ": " + data);
  }
});

本文标签: javascriptparameters in jquery ajax successStack Overflow