简体   繁体   English

当存在4xx或5xx Http错误代码时,在Javascript中解析JSONP响应

[英]Parsing JSONP Response in Javascript when 4xx or 5xx Http Error Code is Present

I am implementing an application which relies upon communication between a JavaScript client and a server which knows how to respond to the client using JSONP notation. 我正在实现一个依赖于JavaScript客户端和服务器之间通信的应用程序,该服务器知道如何使用JSONP表示法响应客户端。

I am attempting to handle the case in my Javascript client where my server returns with an http status code of 4xx or 5xx. 我试图在我的Javascript客户端中处理这种情况,我的服务器返回的http状态代码为4xx或5xx。 Currently what I'm seeing is that the script is not evaluated as the browser believes it to be an error (which it is.) However, I still want to read what my server has to say in the event of this 4xx or 5xx response code in my JavaScript client. 目前我所看到的是脚本没有被评估,因为浏览器认为它是一个错误(它是。)但是,我仍然想要阅读我的服务器在4xx或5xx响应的情况下要说的内容我的JavaScript客户端中的代码。

I'm seeing that this does raise an error on the script tag element, but I'm concerned that this is not cross browser and will not be a robust solution. 我发现这确实引起了脚本标记元素的错误,但我担心这不是跨浏览器,也不会是一个强大的解决方案。

Has anyone had any luck on still parsing a jsonp response even though the http status code is 4xx or 5xx? 即使http状态代码是4xx或5xx,有没有人在解析jsonp响应方面有任何运气?

I'm beginning to believe I should just use this "set a timeout" solution which "detects" a failure by stating the callback function to the jsonp request would complete within a certain time frame, and if it doesn't, there was an error. 我开始相信我应该使用这个“设置超时”解决方案,通过声明jsonp请求的回调函数将在某个时间范围内完成来“检测”失败,如果没有,则有一个错误。

EDIT: I'm temporarily always returning 200 status code when my server detects a jsonp client and then tunneling the error message/status in the json object returned. 编辑:当我的服务器检测到jsonp客户端然后在返回的json对象中隧道传送错误消息/状态时,我暂时总是返回200状态代码。 I was hoping to take advantage of the HTTP status codes but I'm thinking that is no-go for a javscript client. 我希望利用HTTP状态代码,但我认为javscript客户端是不行的。

JSONP is a hack to work-around cross-domain issues. JSONP是解决跨域问题的黑客攻击。 When it works, it works well. 当它工作时,它运作良好。 But when it doesn't you don't have a way to figure out what went wrong. 但是当它没有时,你没有办法弄清楚出了什么问题。

setTimeout is another hack on top of the original one. setTimeout是原始版本之上的另一个hack。 If you must use JSONP and still need error detection (not handling), thats what you'd have to do. 如果你必须使用JSONP并仍然需要错误检测(不处理),那就是你必须要做的。 There isn't a better solution. 没有更好的解决方案。

If you control the server, try to use alternatives such as Cross-Origin-Resource-Sharing (CORS), or Flash's crossdomain.xml to allow cross domain requests. 如果您控制服务器,请尝试使用Cross-Origin-Resource-Sharing(CORS)或Flash的crossdomain.xml等替代方案来允许跨域请求。 If you don't control the server, you can proxy the response through your server to get better control. 如果您不控制服务器,则可以通过服务器代理响应以获得更好的控制。

One approach when using JSONP is to embed status information in the callback. 使用JSONP时的一种方法是在回调中嵌入状态信息。 So the callback function signature would look like 所以回调函数签名看起来像

callback(result, status, message)

So if your call looks like 所以如果你的电话看起来像

http://myurl.com/?callback=fn

generate code for a successful call that looks like 为成功调用生成代码

fn({"data":"my great data"}, 200)

and for an exceptional condition 并为一个特殊的条件

fn(null, 500, "server error"}

You can check the status of the XHR object (if you are not using a JS library). 您可以检查XHR对象的status (如果您没有使用JS库)。

if(xhr.readyState == 4){
  if(xhr.status == 200){
    // good
  }else if(xhr.status == 502){
    // d'oh
  }
}

If you are using jQuery, you can pass in a statusCode to handle special cases for $.ajax 如果您使用的是jQuery,则可以传入statusCode来处理$ .ajax的特殊情况

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

相关问题 5xx 或 4xx 错误,“不存在‘Access-Control-Allow-Origin’标头” - 5xx or 4xx error with “No 'Access-Control-Allow-Origin' header is present” javascript响应未解析为状态码4xx - javascript response not parsed with status code 4xx 在 fetch() Promise 中,当状态为 4xx 或 5xx 时,如何捕获服务器错误消息? - Within a fetch() Promise, how to .catch server errors messages when status is 4xx or 5xx? 如何将 4XX 和 5XX 错误发送给哨兵? - How to send 4XX and 5XX errors to sentry? 可以在控制台中隐藏资源加载的5xx / 4xx错误吗? - Can 5xx/4xx errors for resource loads be hidden in the console? 如何在 fetch api 中处理 HTTP 代码 4xx 响应 - How to handle HTTP code 4xx responses in fetch api 使用 Redux 工具包异步 REST ZDB974238714CA8DE6FZA 模式时如何捕获 HTTP 4xx 错误? - How to trap HTTP 4xx errors when using Redux Toolkit async REST API pattern? 可以用 try catch 块 React 应用程序处理错误(状态代码 4xx)吗 - Can React app handle error (status code 4xx) with try catch block Javascript:时间到xx:xx:xx时单击按钮 - Javascript : Click button when time reach xx:xx:xx 使用then / catch语法通过axios分别捕获4xx和代码错误 - Catching 4xx and code errors separately with axios using then / catch syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM