我有一个脚本,它接收用户的UPS跟踪号码,并通过跨域AJAX请求(使用jQuery.ajax)从UPS JSON Tracking API获取跟踪信息。一切都运作良好,直到大约两周前。
然后,出乎意料的是,由于臭名昭着的" No' Access-Control-Allow-Origin'标头出现在请求的资源上。"错误。
问题是,请求 仍然正常通过。如果我检查Chrome开发工具中的网络标签,我可以在标题中看到状态代码200。单击预览或响应选项卡,我可以看到应该返回的正确JSON响应。
首先,如果我的来源不被允许访问,我很难知道如何获得适当的回复。第二,有没有办法让这个错误发生并仍然可以访问响应?我试过从"错误"中获取xhr对象。和#34;完成"回调(使用jQuery.ajax)但我只能访问错误对象。
我的代码:
$.ajax({
type: "POST",
dataType: "json",
url: url,
data: json,
success: function(data){
cb(data);
},
error: function(xhr, status, error){
console.log(xhr, status, error);
// logs: {readyState: 0, responseText: "", status: 0, statusText: "error"}
},
complete: function(xhr, status){
console.log(xhr, status);
// also logs: {readyState: 0, responseText: "", status: 0, statusText: "error"}
}
});
答案 0 :(得分:1)
实际上这是一个跨域问题,你有多种解决方案可以解决:
了解跨域:CORS
已添加到您的请求标题中
Access-Control-Allow-Origin: *
或者在您使用的浏览器中允许跨域:
Chromium-browser --disable-web-security --user-data-dir
徒步旅行:
Development > Disable Multi-Origin restriction