简体   繁体   English

jquery中的参数ajax成功

[英]parameters in jquery ajax success

I am trying to find out what parameters I can pass to a jQuery ajax call. 我试图找出我可以传递给jQuery ajax调用的参数。

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. 显然,我在成功函数中放入的“响应”变量只收回服务器从ajax调用发回的任何内容。 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? 或者,是否有其他方法来获取http响应代码?

Thanks to whoever can answer this! 感谢谁能回答这个问题!

You can get the response 's status code on the success' third parameter or complete 's first parameter, something like this: 您可以在success'第三个参数或complete的第一个参数上获取responsestatus code ,如下所示:

$.ajax({
  success: function(data, textStatus, xhr) {
    console.log(xhr.status);
  },
  complete: 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.com/jquery.ajax/ . 继@Kokizzu之后,您可以查看jQuery API站点以查看哪些参数传递给其他函数http://api.jquery.com/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中发回json json_encode

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);
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM