2011-09-23 64 views
3

我想在链接megaupload中获得真正的路径,但总是这样,但不工作。xmlhttprequest状态问题302

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); 

} 

我需要这个链接:重定向到:http://www534.megaupload.com/files/c2c36829bc392692525f5b7b3d9d81dd/Coldplay - 警告Sign.mp3

这个insted的:http://www.megaupload.com/?d=6CKP1MVJ

+0

在我看来,这个URL存在(返回200,而不是302)。如果您需要其他链接,为什么不直接使用该链接?或者你是否想为megaupload下载写一些通用的东西? –

+0

是的,我在努力。谢谢,工作。 – user916933

回答

5

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); 

不是说你在这里使用的链接重定向的任何地方,但是这是一般的做法。顺便说一句,而不是查看重定向的响应文本,你应该看看req.getResponseHeader("Location")

+0

这似乎不工作了。在Chrome(29.0)中,我得到'未捕获的ReferenceError:组件未定义,并且在Firefox中(24.0),我得到'TypeError:xhr.channel未定义。 此外,组件上的MDN页面表示您不应再使用它。 https://developer.mozilla.org/en/docs/Components_object – mflodin

+0

@mflodin:这是一个关于** Firefox扩展**的问题。这段代码在Chrome中无法使用并不奇怪。另外,Firefox中的网页不能访问像XMLHttpRequest.channel这样的低级API,这并不令人惊讶。只有特权代码(意味着扩展名)才能访问它。 –

+0

@WladimirPalant啊,对不起。我错过了。有很多关于302问题的堆栈溢出问题。我一定会在将来更仔细地检查标签。 – mflodin