我想从我的AJAX请求获取HTTP响应代码/响应头。这里是我的原创剧本:AJAX请求 - 获取HTTP代码/响应头成功请求
$("#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();
})
})
这是工作的罚款。我已经更新了这个,试图获得HTTP响应代码/头文件并在console.log中查看,但我没有看到任何东西。这里是我的更新脚本:
$("#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();
})
})
但我不会在控制台中得到任何东西(请求成功运行虽然)。我也注意到更新脚本的第二行输出也没有出现在控制台中。
清除浏览器历史记录,然后尝试在有关CORS或许控制台 – JYoThI
什么?或Access-Control- *标题? –
我重新启动浏览器,我现在看到控制台中的条目,但我得到“undefined”值而不是预期值: httpStatus:undefined httpResponseCode:undefined – user982124