简体   繁体   English

AJAX请求-获取成功请求的HTTP代码/响应头

[英]AJAX Request - get HTTP Code/Response Header for successful request

I'm trying to get the HTTP Response Code/Response Header from my AJAX request. 我正在尝试从我的AJAX请求中获取HTTP响应代码/响应头。 Here's my original script: 这是我的原始脚本:

 $("#callContact1").click(function() { $.ajax({ url: "https://www.server.com?type=makecall", data: {}, type: "GET" }) .then(function(data) { $('#ajaxResponse').html(data).show(); }) .fail(function(xhr) { var httpStatus = (xhr.status); var ajaxError = 'There was an requesting the call back. HTTP Status: ' + httpStatus; console.log('ajaxError: ' + ajaxError); //make alert visible $('#ajaxResponse').html(ajaxError).show(); }) }) 

which is working fine. 工作正常。 I've updated this to try and get the HTTP response code/header and view this in the console.log but I'm not seeing anything there. 我已经对此进行了更新,以尝试获取HTTP响应代码/标头并在console.log中查看此信息,但在那里什么都看不到。 Here's my updated script: 这是我更新的脚本:

 $("#callContact1").click(function() { console.log('starting call back request'); $.ajax({ url: "https://www.server.com?type=makecall", data: {}, type: "GET" }) .then(function(data) { $('#ajaxResponse').html(data).show(); var httpStatus = (data.status); var httpResponseCode = (data.getAllResponseHeaders); console.log('httpStatus: ' + httpStatus); console.log('httpResponseCode: ' + httpResponseCode); }) .fail(function(xhr) { var httpStatus = (xhr.status); var ajaxError = 'There was an requesting the call back. HTTP Status: ' + httpStatus; console.log('ajaxError: ' + ajaxError); //make alert visible $('#ajaxResponse').html(ajaxError).show(); }) }) 

but I'm not getting anything in the console (the request is executed successfully though). 但我在控制台中什么都没得到(尽管请求已成功执行)。 I also noticed the output from the 2nd line of the updated script is also not appearing in the console either. 我还注意到更新的脚本第二行的输出也未出现在控制台中。

Modify the above code to 修改上面的代码为

.then(function(data,status,xhr) {
      $('#ajaxResponse').html(data).show();
      var httpStatus = status;
      var httpResponseCode = (xhr.status);
      console.log('httpStatus: ' + httpStatus);
      console.log('httpResponseCode: ' + httpResponseCode);
    })

As you can find in jQuery's documentation for jQuery.ajax() 您可以在jQuery的jQuery.ajax()文档中找到

https://api.jquery.com/jQuery.ajax#jqXHR https://api.jquery.com/jQuery.ajax#jqXHR

you can use .done() and .fail() or using .then() which incorporates both, using two callback functions as parameters, the first for success and the second for fail. 可以使用.done()和.fail()或使用。然后() ,其既包含,使用两个回调函数作为参数,所述第一成功,第二个用于FAIL。

So, you could use smt like this: 因此,您可以像这样使用smt:

.then(function(data, status, xhr) {
    $('#ajaxResponse').html(data).show();
    var httpStatus = status;
    var httpResponseCode = (xhr.status);
    console.log('httpStatus: ' + httpStatus);
    console.log('httpResponseCode: ' + httpResponseCode);
}, function(data, status, xhr) {
    var ajaxError = 'There was an requesting the call back. HTTP Status: ' + status;
    console.log('ajaxError: ' + ajaxError); //make alert visible 
    $('#ajaxResponse').html(ajaxError).show();

})

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

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