是否可以通过Ajax获取页面重定向信息? [英] Is it possible to get page redirect information via Ajax?

查看:230
本文介绍了是否可以通过Ajax获取页面重定向信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有ajax,如果我们加载http://example.com/1并将其重定向到http://example.com/2,则浏览器将获取适当的标头,并且浏览器URL将得到更新.有没有办法通过jQuery Ajax获取此信息?

Without ajax, if we load http://example.com/1 and if it redirects to http://example.com/2 then the browser gets appropriate headers and the Browser URL get's updated. Is there a way to get this information via jQuery Ajax?

例如,我正在通过Ajax请求http://api.example.com.在PHP中,此页面重定向到http://api2.example.com.有可能知道这件事吗?

For example, I am requesting http://api.example.com via Ajax. In PHP, this page is redirected to http://api2.example.com. Is it possible to know this thing?

使用: 我有一个带有链接的导航栏.所有页面均通过AJAX 加载到容器中,并且我根据链接使用HTML5历史记录将网址推送到浏览器栏上.

Use: I have a navbar which has links. All pages are loaded into the container via AJAX and I push the url on Browser Bar using HTML5 history as per the link.

但是,如果页面被重定向,页面将具有新的链接吗?我也想在浏览器栏中更改它.我想知道在重定向Ajax URL的地方.

However, if the page gets redirected, the page would have a new link right? I would like to change that in the Browser bar too. I would like to know where the Ajax URL is redirected in case it is redirected.

为什么这很重要? 我的链接处理表单数据,请求和各种身份验证.例如,如果我请求https://oauth.example.org?code=56hycf86,它可以重定向到successfailure页面.我的Ajax可以获取正确的html内容,但URL浏览器栏上的URL仍带有相同的Auth ID,如果重新加载,则会产生错误.还有其他安全问题.

Why this is important? My links handle form data, requests and various authentications. For example, if I request, https://oauth.example.org?code=56hycf86 it either redirects to success or failure page. My Ajax get the right html content but the URL browser bar still has the URL with same Auth ID which, if reloaded, produces error. There are other security issues too.

我不知道我的解释是否正确,但感谢您的帮助.

I do not know if I explained things right, but thanks for your help.

推荐答案

很遗憾,ajax始终遵循重定向.但是,并非所有浏览器都支持此功能,您可以访问XMLHttpRequest对象的responseURL属性.

Well, unfortunately, ajax always follows redirects. However, there is a feature that is not supported in all browsers, you can access the responseURL property of the XMLHttpRequest object.

您可以在下面的代码段中尝试一下.重定向按钮将ajax请求发送到以1个重定向答复的URL(如果有多个重定向,它也可以使用). 没有重定向"按钮将ajax请求发送到没有重定向的URL.

You can try it in the below code snippet. the redirect button sends ajax request to a URL that replies with 1 redirect (it also works if there are multiple redirects). The no redirect button sends ajax request to a URL with no redirects.

据我所知,IE 11和旧版本的chrome/firefox/opera浏览器不支持此方法.

As far as I know this method is not supported in IE 11 and older versions of chrome/firefox/opera browsers.

document.getElementById("no-redirect").addEventListener("click", function() {
  testRedirect("https://httpbin.org/get");
});

document.getElementById("redirect").addEventListener("click", function() {
  testRedirect("https://httpbin.org/redirect/1");
});


function testRedirect(url) {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function(e) {
    if (xhr.status == 200 && xhr.readyState == 4) {
      if (url != xhr.responseURL) {
        alert("redirect detected to: " + xhr.responseURL)
      } else {
        alert("no redirect detected")

      }
    }
  }
  xhr.open("GET", url, true);
  xhr.send();
}

<button id="redirect">
  Redirect
</button>
<button id="no-redirect">
  No Redirect
</button>

这篇关于是否可以通过Ajax获取页面重定向信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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