捕302发现的JavaScript [英] Catching 302 FOUND in JavaScript

查看:121
本文介绍了捕302发现的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery作出AJAX POST请求到我的服务器,它可以返回与状态302 HTTP响应随后的JavaScript只是发送GET请求到该网址,而我想重定向用户到URL中的这种反应。这可能吗?

I use jQuery to make an AJAX POST request to my server, which can return HTTP response with status 302. Then JavaScript just sends GET request to this URL, while I'd like to redirect user to URL in this response. Is this possible?

推荐答案

接受的答案不会由于上述原因的工作。我张贴的链接到所描述的黑客避开了302的问题被透明地由浏览器来处理一个问题的注释:

The accepted answer does not work for the reasons given. I posted a comment with a link to a question that described a hack to get round the problem of the 302 being transparently handled by the browser:

<一个href="http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call">http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call

不过,这是一个有点脏的黑客攻击,并经过多次周围挖我找到了我认为这是一个更好的解决方案 - 利用 JSON 。在这种情况下,你可以使所有响应Ajax请求有code 200,并在回复的正文中,添加某种JSON对象,你的Ajax响应处理程序,然后在适当的方式使用。

However, it is a bit of a dirty hack and after much digging around I found what I think is a better solution - use JSON. In this case, you can make all responses to ajax requests have the code 200 and, in the body of the response, you add some sort of JSON object which your ajax response handler can then use in the appropriate manner.

这篇关于捕302发现的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆