2016-08-27 69 views
0

我想通過點擊下載按鈕使用api來抓取使用javascript的網頁。但我沒有收到任何responseText在這個和相同的API使用curl工作。XMLHttpRequest嘗試抓取時無法正常工作

download.addEventListener('click',function(){ 

    document.getElementById('check').innerHTML = url; 

    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + url + '"') + '&format=json&callback=?'; 
    document.getElementById('check').innerHTML = yql; 
    var request = new XMLHttpRequest(); 


    request.onreadystatechange = function(){ 
     document.getElementById('check').innerHTML= request.readyState; 

     if(request.readyState ===XMLHttpRequest.DONE) { 
     document.getElementById('check').innerHTML= request.responseText; 
     } 
    }; 

    request.open("GET", yql, true); 

    request.send; 

},false); 
+1

不應該有'request.send()'? – Seb

回答

-1

單從YPL刪除回調,然後此代碼工作正常:

download.addEventListener('click',function(){ 

    document.getElementById('check').innerHTML = url; 


    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + url + '"') + '&format=jsonp'; 
    document.getElementById('check').innerHTML = yql; 


    var request = new XMLHttpRequest(); 
    request.open("GET", yql, true); 
    request.send(); 

    request.onreadystatechange = function(){ 
     document.getElementById('check').innerHTML= request.readyState + request.status; 
     if(request.readyState ===4) { 
      document.getElementById('check').innerHTML= request.responseText; 
     } 
    }; 


},false); 
相關問題