javascript xmlhttprequest 状态 302 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7524039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems with xmlhttprequest status 302
提问by ericmoraess
I am trying to get real path in link megaupload but always but this dont work.
我试图在链接 megaupload 中获得真实路径,但总是这样,但这不起作用。
function getRealURL(){
var st = new String("");
var req = new XMLHttpRequest();
req.open("GET","http://www.megaupload.com/?d=6CKP1MVJ",true);
req.send(null);
req.send(null);
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 302){
//SUCESSO
st = req.responseText;
}
}
};//funcao
element.getElementById("id").setAttribute("value", st);
}
i need this link:
我需要这个链接:
Redirect to: http://www534.megaupload.com/files/c2c36829bc392692525f5b7b3d9d81dd/Coldplay - Warning Sign.mp3
insted of this:
这样做的:
http://www.megaupload.com/?d=6CKP1MVJ
回答by Wladimir Palant
XMLHttpRequest
follows the redirect automatically by default so you don't see the 302 response. You need to set nsIHttpChannel.redirectionLimitproperty to zero to prevent it:
XMLHttpRequest
默认情况下会自动跟随重定向,因此您看不到 302 响应。您需要将nsIHttpChannel.redirectionLimit属性设置为零以防止它:
req.open("GET","http://www.megaupload.com/?d=6CKP1MVJ",true);
req.channel.QueryInterface(Components.interfaces.nsIHttpChannel).redirectionLimit = 0;
req.send(null);
Not that the link you use here redirects anywhere but this is the general approach. Btw, instead of looking at the response text for redirects you should look at req.getResponseHeader("Location")
.
并不是您在此处使用的链接重定向到任何地方,但这是一般方法。顺便说一句,与其查看重定向的响应文本,不如查看req.getResponseHeader("Location")
.