jquery - 如何使用 AJAX 请求获取重定向 URL?

标签 jquery ajax redirect

我有一个问题..在我的项目中,我需要向 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/

相关文章:

javascript - JQuery slim scroll 不会滚动到最后一个元素

javascript - 单击链接后的页面转换延迟(动画结束后)

jquery - 突出显示 Web 元素中的特定文本

php - AJAX 请求返回 JSON,不工作

ruby-on-rails - Rails JS 响应,同时呈现状态和模板?

php - 在 PHP 中重定向之前的等待时间

c# - Url.PathAndQuery 出现错误

javascript - Symfony 3 Form Ajax Post请求数据为空

php - 包含带有 .htaccess 的子文件夹

javascript - 优化重定向代码