2016-12-20 35 views
-3

變量在網頁我想收集來自另一個Web服務器在給定的URL地址的響應。保存網址響應到javascript/html

假設其他人有一臺服務器http://mysite/123,它使用簡單的字符串進行響應。 (沒有標題和東西)。

什麼是最簡單的方法來獲取我的網頁上的JavaScript來收集URL的原始響應,最好是一個字節數組變量?雖然我會除了一個答案,保存在字符串讓我去。這是從我的HTML文檔精確複製粘貼,它不適合我。

謝謝!

<script> 

var txt = ""; 

txt=httpGet("https://www.google.com"); 
alert(txt.length.toString()); 

function httpGet(theUrl) { 
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp = new XMLHttpRequest(); 
    } 
    else {// code for IE6, IE5 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      return xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET", theUrl, false); 
    xmlhttp.send(); 
} 
</script> 

回答

-1

所以我不得不說,你最好的選擇是尋找從JavaScript製作HTTP(或XHR)請求。 檢查:Return HTML content as a string, given URL. Javascript Function

function httpGet(theUrl) 
{ 
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else 
    {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      return xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET", theUrl, false); 
    xmlhttp.send();  
} 
+0

我加入的正是我粘貼到問題的品牌新的HTML文檔......我不是得到任何東西複製/粘貼。任何想法我做錯了什麼? – TaterKing