Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

javascript - Problems with xmlhttprequest status 302

I am trying to get real path in link megaupload but always but this dont work.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

XMLHttpRequest follows the redirect automatically by default so you don't see the 302 response. You need to set nsIHttpChannel.redirectionLimit property to zero to prevent it:

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").


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...