简体   繁体   English

如何检查是否没有“ Access-Control-Allow-Origin”标头错误

[英]How to check for No 'Access-Control-Allow-Origin' header error

When using XMLHttpRequest in JavaScript, is it possible to distinguish between the following two errors (GET completely failed / No 'Access-Control-Allow-Origin' header)? 在JavaScript中使用XMLHttpRequest时,是否可以区分以下两个错误(GET完全失败/没有“ Access-Control-Allow-Origin”标头)?

错误日志

Obviously, readyState and status of the XMLHttpRequest object don't differ. 显然,readyState和XMLHttpRequest对象的状态没有区别。 I tried to use window.onerror to catch all errors but apparently, these two errors do not trigger the callback function. 我尝试使用window.onerror捕获所有错误,但是显然,这两个错误不会触发回调函数。

Your best course of action would be to prevent this error from even occurring in the first place. 最好的做法是防止此错误发生在最开始。 By making sure the server adds the domain you're trying to access from your script to the Access-Control-Allow-Origin setting you don't have to deal with it client-side. 通过确保服务器将您尝试从脚本中访问的域添加到Access-Control-Allow-Origin设置中,您无需在客户端进行处理。

That said, you should be able to attach a handler to the error event of the XMLHttpRequest object. 也就是说,您应该能够将处理程序附加到XMLHttpRequest对象的错误事件。 Using the error message you might be able determine what went wrong; 使用错误消息,您可能能够确定出了什么问题。 but that is of lesser interest. 但这没那么有趣。 It doesn't matter to the client what went wrong, only that it isn't going to get it's data, so you can plan accordingly. 对客户端来说,出什么错并不重要,只是它不会获取数据,因此您可以相应地进行计划。

var oReq = new XMLHttpRequest();

oReq.addEventListener("error", transferFailed);

oReq.open();

function transferFailed(evt) {
  console.log("An error occurred while transferring the file.");
}

More information: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Asynchronous_request 详细信息: https : //developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Asynchronous_request

暂无
暂无

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

相关问题 SAPUI5 中的 Access-Control-Allow-Origin' 标头错误 - Access-Control-Allow-Origin' header error in SAPUI5 Google翻译时出现XMLHttpRequest错误(没有'Access-Control-Allow-Origin'标题) - XMLHttpRequest error with Google translate (No 'Access-Control-Allow-Origin' header) Zillow API错误:“不存在“ Access-Control-Allow-Origin”标头” - Zillow API Error:“No 'Access-Control-Allow-Origin' header is present” 错误在请求的资源上没有“ Access-Control-Allow-Origin”标头 - Error No 'Access-Control-Allow-Origin' header is present on the requested resource AJAX请求中没有“ Access-Control-Allow-Origin”标头存在错误 - No 'Access-Control-Allow-Origin' header is present error in AJAX Request jQuery.getJSON()上没有'Access-Control-Allow-Origin'标头错误 - No 'Access-Control-Allow-Origin' header Error on jQuery.getJSON() “没有'Access-Control-Allow-Origin'标题存在”与Cherrypy错误 - “no 'Access-Control-Allow-Origin' header is present” error with Cherrypy 在某些情况下没有“ Access-Control-Allow-Origin”标头错误,但在其他情况下则没有 - No 'Access-Control-Allow-Origin' header error in some cases, but not in others No 'Access-Control-Allow-Origin' header is present on the requested resource error - No 'Access-Control-Allow-Origin' header is present on the requested resource error $ .post没有'Access-Control-Allow-Origin'标头 - $.post No 'Access-Control-Allow-Origin' header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM