返回重定向作为对 XHR 请求的响应 [英] Returning redirect as response to XHR request

查看:39
本文介绍了返回重定向作为对 XHR 请求的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果浏览器收到对 ajax 请求的重定向响应会怎样?

What happens if the browser receives a redirect response to an ajax request?

推荐答案

如果浏览器收到对 ajax 请求的重定向响应会怎样?

如果服务器发送重定向(又名 302 响应加上 Location: 标头),浏览器会自动跟随重定向.对第二个请求的响应(假设它也不是另一个重定向)是向您的程序公开的内容.

If the server sends a redirect (aka a 302 response plus a Location: header) the redirect is automatically followed by the browser. The response to the second request (assuming it also isn't another redirect) is what is exposed to your program.

实际上,您无法检测是否发生了 302 响应.如果 302 重定向导致 200,那么您的程序的行为与原始请求直接导致 200 相同.

In fact, you don't have the ability to detect whether a 302 response has occurred. If the 302 redirect leads to a 200, then your program acts identically as if the original request led directly to a 200.

这既是我的经验,也是 规范中提到的行为.

This has been both my experience and the behavior called out in the spec.

2016 更新: 时间过去了,好消息是新的 fetch() API 专门提供 细粒度控制如何处理重定向,默认行为类似于 XHR.也就是说,它仅适用于本地实现的 fetch().fetch() 的 Polyfill 版本——基于 XHR——继续有 XHR 的限制.幸运的是,本机浏览器支持 似乎很完善.

2016 Update: Time has passed, and the good news is that the new fetch() API is spec'd to offer finer-grained control of how redirects are handled, with default behavior similar to XHR. That said, it only works where fetch() is implemented natively. Polyfill versions of fetch()—which are based on XHR—continue to have XHR's limitations. Fortunately, native browser support seems to be rounding out nicely.

这篇关于返回重定向作为对 XHR 请求的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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