鍍金池/ 問答/HTML/ xhr 或 jQuery ajax, Post 請求如何獲得 303 狀態(tài)的返回

xhr 或 jQuery ajax, Post 請求如何獲得 303 狀態(tài)的返回結(jié)果

問題網(wǎng)站
http://www.xingk.cc/forum.php...
需注冊,比較麻煩,下面給一個臨時賬號
賬號:愛玩屎的阿拉蕾
密碼:Av123456!

問題描述
論壇的網(wǎng)盤地址被加了跳轉(zhuǎn)鏈,而且是隨機(jī)生成的,跳轉(zhuǎn)鏈?zhǔn)鞘褂?303 重定向到網(wǎng)盤。

需求結(jié)果
有辦法能夠獲得這個跳轉(zhuǎn)鏈返回的 303 狀態(tài)的重定向結(jié)果嗎?
jQuery 的 success好像只有在stats為200才會執(zhí)行。
xhr 無論是 onreadystatechange 還是 error 還是 onload,都沒有得到重定向的結(jié)果。

測試代碼
jQuery

    var jq=document.createElement("script");
    jq.src="http://cdn.staticfile.org/jquery/2.1.4/jquery.min.js";
    document.body.appendChild(jq);
    
    function getQueryString(name,url) {
      url=(url&&url.match(/\?.*/).toString())||location.search;
        var reg = new RegExp("(?:^|&)(" + name + ")=([^&]*)(?:&|$)", "i");
        var str = url.substr(1).match(reg);
        if (str != null) return unescape(str[2]);
        return null;
    }
    
$('a[href*="linkFilter.php"]').each(function(){
$.ajax({
url:"linkFilter.php",
type:'post',
traditional:true,
data:this.href.replace(/.+\?(.+)/,'$1')+"&mod=decode",
contentType:"application/x-www-form-urlencoded",
processData: false,
success:function(e,s,t,d){
console.log(s,t,e);
},
})
})

圖片描述

原生 xhr

var url=document.querySelector('a[href*="linkFilter.php"]').href;
        function getQueryString(name,url) {
          url=(url&&url.match(/\?.*/).toString())||location.search;
            var reg = new RegExp("(?:^|&)(" + name + ")=([^&]*)(?:&|$)", "i");
            var str = url.substr(1).match(reg);
            if (str != null) return unescape(str[2]);
            return null;
        }
var xhr= new XMLHttpRequest();
    //異步接受響應(yīng)
    xhr.onreadystatechange = function(e){
console.log(xhr.readyState, xhr.status, e, xhr.responseURL);
    }
xhr.onload= function(e){
console.log('load', e, xhr.responseURL);
}
xhr.onerror = function(e){
console.log('error', e, xhr.responseURL);
}
    //發(fā)送請求
    xhr.open('post','linkFilter.php',true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
xhr.send("mod=decode&"+getQueryString('code',url)+"&"+getQueryString('tk',url));

圖片描述

回答
編輯回答
久礙你

XMLHttpRequest.responseURL屬性返回經(jīng)過任意多次重定向后的最終URL。

https://developer.mozilla.org...

那個CORS錯誤是肯定有的,可以不管。不過我猜你是想做個自己用的腳本?瀏覽器插件會給跨域xhr給你用,不過我推薦輕量的油猴腳本。

2017年6月30日 18:51
編輯回答
無標(biāo)題

原生的XMLHttpRequest可以利用 request.readyStaterequest.status進(jìn)行判斷。

2018年5月7日 06:01
編輯回答
尕筱澄

既然要 success 里找不到你要的東西,為啥不試試 fail 或者 complete 呢

2018年3月25日 08:29