2012-07-08 24 views
0

我試圖獲得與AJAX頁面的內容和我沒有得到任何結果..阿賈克斯的XMLHttpRequest

這是代碼:

<script type="text/javascript"> 
function onSumResponse() { 
var xmlhttp; 
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) 
    { 
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","http://www.arihav.com/",true); 
xmlhttp.send(); 
} 

</script> 

這是體內的div:

<div id="myDiv"><h2>Let AJAX change this text</h2></div> 
<button type="button" onclick="onSumResponse()">Change Content</button> 

我點擊後得到的是空格div。

編輯:此代碼從w3schools

EDIT 2拍攝:這是在VBScript,工程代碼:

GotothisURL = "http://www.arihav.com" 
Set GetConnection = CreateObject("Microsoft.XMLHTTP") 
GetConnection.Open "get", GotothisURL, False 
GetConnection.Send 
ResponsePage = GetConnection.responseText 
+1

爲什麼不使用JQuery? – rkosegi 2012-07-08 14:21:16

+0

你是否檢查了來自ajax調用的響應?你甚至可以獲得任何數據嗎? – Tomer 2012-07-08 14:21:34

+2

這可能是一個跨域請求,所以它可能無法正常工作。此外,你有一個「IE6,IE5代碼」評論的事實是不好的。 – Ryan 2012-07-08 14:21:35

回答

0

AJAX不會允許訪問不同的服務器,由於origin policy如果你想請求另一個主機更好地使用curl或JSON