在 JavaScript 中捕获 302 FOUND

发布于 2024-07-09 17:22:26 字数 124 浏览 6 评论 0原文

我使用 jQuery 向我的服务器发出 AJAX POST 请求,该请求可以返回状态为 302 的 HTTP 响应。然后 JavaScript 仅向此 URL 发送 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

各自安好 2024-07-16 17:22:26

由于给出的原因,接受的答案不起作用。 我发表了一条评论,其中包含一个问题的链接,该问题描述了一种解决浏览器透明处理 302 问题的方法:

如何在 jQuery Ajax 调用后管理重定向请求

然而,这是一个有点肮脏的黑客,经过大量挖掘后我找到了我认为更好的解决方案 - 使用 JSON。 在这种情况下,您可以使对 ajax 请求的所有响应都具有代码 200,并且在响应正文中添加某种 JSON 对象,然后您的 ajax 响应处理程序可以以适当的方式使用该对象。

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:

How to manage a redirect request after a jQuery Ajax call

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.

放手` 2024-07-16 17:22:26

我不这么认为。 W3C 规定 HTTP 会使用某些状态代码进行重定向,包括 302,必须透明地遵守。 引述如下:

如果响应是 HTTP 重定向(状态代码 301、302、303 或
307),那么必须透明地遵循它(除非它违反了
安全或无限循环预防措施)。 任何其他错误(包括
401) 必须使对象使用该错误页面作为响应。

作为实验,我尝试从各种浏览器(Firefox 3.5、Chrome、IE8、IE7、IE6)向服务器发出 Ajax 请求,给出 302 状态代码,并在浏览器的请求对象中显示状态。 在每种情况下,它都显示为 200。

I don't think so. The W3C says that HTTP redirects with certain status codes, including 302, must be transparently followed. Quoted below:

If the response is an HTTP redirect (status code 301, 302, 303 or
307), then it MUST be transparently followed (unless it violates
security or infinite loop precautions). Any other error (including a
401) MUST cause the object to use that error page as the response.

As an experiment, I tried doing Ajax requests from various browsers (Firefox 3.5, Chrome, IE8, IE7, IE6) to a server giving a 302 status code, and showing the status in the browser's request object. In every case, it showed up as 200.

奢华的一滴泪 2024-07-16 17:22:26

在我的问题中,原因是:

我使用 localhost/Home/Test 地址来测试页面。 但是ajax请求代码使用127.0.0.1/Home/AjaxRequest作为url参数。 当 url 不同时,就会发生此错误。

也许它对某人有帮助:)

In my problem reason was:

i was using localhost/Home/Test addres for testing the page. But ajax request code using 127.0.0.1/Home/AjaxRequest for url parameter. When the urls are different this error occurs.

maybe it helps someone :)

鯉魚旗 2024-07-16 17:22:26

与其要求 Javascript 代码处理 302,不如在 302 事件上返回带有自定义错误代码+消息的 500

Rather than asking the Javascript code to Handle 302, it would be better to return a 500 with custom error code+ message on event of 302

江挽川 2024-07-16 17:22:26
function doAjaxCall() {
   $.ajaxSetup({complete: onRequestCompleted});
   $.get(yourUrl,yourData,yourCallback);
}

function onRequestCompleted(xhr,textStatus) {
   if (xhr.status == 302) {
      location.href = xhr.getResponseHeader("Location");
   }
}
function doAjaxCall() {
   $.ajaxSetup({complete: onRequestCompleted});
   $.get(yourUrl,yourData,yourCallback);
}

function onRequestCompleted(xhr,textStatus) {
   if (xhr.status == 302) {
      location.href = xhr.getResponseHeader("Location");
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文