2017-03-07 72 views
0

我正在嘗試使用XMLHTTP獲取PDF文件,並使用XMLHTTP獲取響應。獲得部分作品,但郵政部分沒有得到迴應。XMLHTTP發佈不起作用

var Req = new XMLHttpRequest(); 
Req.open("POST",'http://192.168.56.103/API/Twebservice.asmx/Updatepdf', false); 
Req.onload = function (oEvent) { 
    // Uploaded. 


var blob = function(){var xhr = new XMLHttpRequest() 
xhr.open("GET", "http://www.pdf995.com/samples/pdf.pdf",true); 
xhr.send(); 

if (xhr.status === 200) { 
var test=xhr.responseText;//console.log(test) 

}} } 
//GetPDF(); 
Req.send(blob()); 

希望有人能幫上忙。

+1

'Req.send(blob());'沒有任何意義。你實際上在做'Req.send(undefined);'你認爲blob()在做什麼? – epascarello

+0

這只是我的嘗試,但我認爲由於異步性質...我無法獲得響應文本到一個變量 – user1222256

+0

因此,您在獲取數據後發出呼叫,但您在代碼中面臨的問題是相同的來源政策 – epascarello

回答

0

將呼叫視爲異步。在第一次完成後調用第二個。

function firstCall() { 
    var xhr = new XMLHttpRequest() 
    xhr.open("GET", "path1", true); 
    xhr.onload = function() { 
    secondCall(xhr.responseText); 
    }; 
    xhr.onerror = function() { 
     console.error("Error", xhr.statusText); 
    }; 
    xhr.send(); 
} 

function secondCall(data) { 
    var xhr = new XMLHttpRequest() 
    xhr.open("POST", "path2", true); 
    xhr.onload = function() { 
    console.log("done");  
    }; 
    xhr.onerror = function() { 
     console.error("Error", xhr.statusText); 
    }; 
    xhr.send(data); 
} 
+0

謝謝Epascarello ...修復它。現在我知道我要去哪裏錯了。 – user1222256