我有一个问题..在我的项目中,我需要向 URL 缩短网站(例如 bit.ly 等)发出 AJAX 请求。
我的目标是获取重定向 URL 或重定向站点的内容。到目前为止,我尝试了网上的一些技术后,仍然无法得到正确的结果。
我 try catch 301 HTTP 响应状态(URL 缩短网站的默认行为),但我发现请求永远不会返回 301。奇怪的是,它返回 200。
知道如何解决这个问题吗?非常感谢..:)
最佳答案
根据spec引用于this answer将自动遵循 30x header ,并且无法从 Ajax 调用中获取重定向资源:
The same-origin request event rules are as follows:
If the response has an HTTP status code of 301, 302, 303, or 307 If the redirect violates infinite loop precautions this is a network error.
Otherwise, run these steps:
Set the request URL to the URL conveyed by the Location header.
If the XMLHttpRequest origin and the origin of request URL are same origin transparently follow the redirect while observing the same-origin request event rules.
Otherwise, follow the cross-origin request steps and terminate the steps for this algorithm.
您可能需要为此使用服务器端解决方案。
关于jquery - 如何使用 AJAX 请求获取重定向 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7672076/