如何使用AJAX请求获取重定向URL? [英] How to get redirect URL using an AJAX Request?

查看:1150
本文介绍了如何使用AJAX请求获取重定向URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题..在我的项目中,我需要向URL缩短站点(例如bit.ly等)提出AJAX请求.

I have a problem.. in my project, i need to make an AJAX request to a URL shortener site (such as bit.ly, etc..).

我的目标是获取重定向URL或重定向站点的内容.到目前为止,在尝试了一些网络技术之后,我仍然无法获得正确的结果.

My target is either I could get the redirect URL or the content of the redirected site. This far, after I tried some techniques from the web, I still cannot get the correct result.

我试图捕获301 HTTP响应状态(URL缩短站点的默认行为),但是我发现请求从未返回301.奇怪的是,它返回了200.

I tried to catch the 301 HTTP response status (the default behaviour of URL shortener sites), but I found that the request never returns 301. Strangely, it returns 200.

有什么办法解决这个问题吗?非常感谢..:)

Any idea how to solve this? Thanks a lot.. :)

推荐答案

根据此答案中引用的方法" rel ="nofollow noreferrer">规范 将自动遵循30x标头,并且无法从Ajax调用中获取重定向资源:

According to the spec quoted in this answer a 30x header will automatically be followed and there is no way to get hold of the redirecting resource from within an Ajax call:

同源请求事件规则如下:

The same-origin request event rules are as follows:

如果响应的HTTP状态代码为301、302、303或307 如果重定向违反了无限循环预防措施,则这是网络错误.

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:

  • 将请求URL设置为Location标头传达的URL.

  • Set the request URL to the URL conveyed by the Location header.

如果XMLHttpRequest的起源和请求URL的起源是相同的起源,则在遵循相同起源的请求事件规则的同时,透明地遵循重定向.

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.

您可能需要为此使用服务器端解决方案.

You will probably need to use a server-side solution for this.

这篇关于如何使用AJAX请求获取重定向URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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