javascript 如何处理 CORS 错误代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26695017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle CORS error code?
提问by colllin
Edit: I just realized this is a duplicate of Recommended solution for AJAX, CORS, Chrome & HTTP error codes (401,403,404,500), and he tried the idea I propose at the end. But I can't tell if he succeeded (dud user?), and no one else has posted a solution or even a comment, so I think it's worth fishing for new answers.
编辑:我刚刚意识到这是AJAX、CORS、Chrome 和 HTTP 错误代码 (401,403,404,500)的推荐解决方案的副本,他在最后尝试了我提出的想法。但我不知道他是否成功了(愚蠢的用户?),并且没有其他人发布了解决方案甚至评论,所以我认为值得寻找新的答案。
Problem:
问题:
- I send a properly-executed (edit: IMproperly-executed. End of story...) CORS request.
- The server receives the request and attempts to process it.
- The server returns an error response, for example a
422 Unprocessable Entity
, along with JSON information about the errors. The idea is that my app could receive this error information and handle it appropriately in the UI. - The browser blocks my error handler from getting the response content, or even getting the status code.
- 我发送了一个正确执行的(编辑:IMproperly-executed。故事结束......)CORS 请求。
- 服务器接收请求并尝试处理它。
- 服务器返回错误响应,例如 a
422 Unprocessable Entity
,以及有关错误的 JSON 信息。这个想法是我的应用程序可以接收此错误信息并在 UI 中适当地处理它。 - 浏览器阻止我的错误处理程序获取响应内容,甚至获取状态代码。
Showing that the browser received the 401
status code but treated it as a CORS security error:
显示浏览器收到401
状态代码但将其视为 CORS 安全错误:
The response object, showing that my code cannot access the response data (data: ""
, status: 0
):
响应对象,显示我的代码无法访问响应数据 ( data: ""
, status: 0
):
How have other people handled this limitation? My best guess right now is to hiHyman an HTTP "success" code (2XX
) as an error code, and then include the error information in the response. This prevents me from using the ajax error handlers in a normal way, but I'm handling this as a global ajax filter anyway, so this filter would capture the deviant success code and trigger the error handlers instead.
其他人是如何处理这个限制的?我现在最好的猜测是劫持 HTTP“成功”代码 ( 2XX
) 作为错误代码,然后在响应中包含错误信息。这使我无法以正常方式使用 ajax 错误处理程序,但无论如何我都将其作为全局 ajax 过滤器处理,因此该过滤器将捕获异常成功代码并改为触发错误处理程序。
采纳答案by Trott
The console message indicates that the server isn't sending the required Access-Control-Allow-Origin
header when it sends the 401 response code.
控制台消息表明服务器Access-Control-Allow-Origin
在发送 401 响应代码时没有发送所需的标头。
You won't be able to use the CORS error handler to inject content into the DOM unless you fix that.
除非您修复该问题,否则您将无法使用 CORS 错误处理程序将内容注入 DOM。
The server is likely sending the header correctly on responses with a 200 response code. It needs to do it for other response codes, though, if you wish to use data from those response codes.
服务器很可能在响应中正确发送了 200 响应代码的标头。但是,如果您希望使用来自这些响应代码的数据,则需要对其他响应代码执行此操作。
Fix that on the server end before making design compromises on the client side. That may solve your problem straight away.
在客户端进行设计妥协之前,先在服务器端修复它。这可能会立即解决您的问题。
回答by Andrew Taylor
It seems it's an opaque response where you can't obtain the headers or the response. And everything is set to null or empty.
似乎这是一个不透明的响应,您无法获得标头或响应。并且一切都设置为空或空。
https://developer.mozilla.org/en-US/docs/Web/API/Response/type
https://developer.mozilla.org/en-US/docs/Web/API/Response/type
Or maybe in the server you should add:
或者也许在服务器中你应该添加:
Access-Control-Allow-Origin: *
回答by progyammer
Very late answer but in case someone wants to check whether an error occurred while sending an XMLHttpRequest
and then take appropriate actions (on the CLIENT side), then this is a quick workaround:
很晚的答案,但如果有人想检查发送时是否发生错误XMLHttpRequest
,然后采取适当的措施(在客户端),那么这是一个快速的解决方法:
try{
request.send();
}catch(err){
if(e.toString().startsWith("NetworkError")){
//pasre the string to check error code
//and take appropriate actions
}
}
This is needed because the onreadystatechange
function doesn't get executed when a NetworkError
occurs and, in fact, the whole script is terminated.
这是必需的,因为onreadystatechange
当 aNetworkError
发生时函数不会被执行,事实上,整个脚本都被终止了。