0
使某些AJAX腳本工作時遇到一些問題。 以此爲HTML頁面:使AJAX工作的問題
<html>
<head>
<script type="text/javascript" src="scripts\main.js"></script>
<link rel="stylesheet" type="text/css" href="css\mainStyle.css" />
</head>
<body onload="onloadHandler();">
<canvas id="canvas" style="background-color:#ddd">
Sorry, your browser does not support the canvas element.
</canvas>
</body>
</html>
腳本:
function onloadHandler()
{
canvasItem = document.getElementById('canvas');
canvasItem.addEventListener('mousedown', mousedownEventHandler, false);
canvasItem.addEventListener('mousemove', mousemoveEventHandler, false);
callService("http://www.xul.fr/somefile.xml");
// callService("http://allcodecorner.com/index.html");
// callService("http://stackoverflow.com");
// callService("http://www.w3schools.com/ajax/ajax_info.txt");
setTimeout("redraw()", 5);
}
function callService(serviceName)
{
var xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState == 0)
{
alert('not initialized');
} else if (xmlhttp.readyState == 1)
{
alert('connection established');
} else if (xmlhttp.readyState == 3)
{
alert('processing request');
} else if (xmlhttp.readyState == 4)
{
alert('ready!');
}
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
alert(xmlhttp.responseText);
} else {
alert('Failed: ' + xmlhttp.responseText + ", status: " + xmlhttp.status);
}
}
}
alert(serviceName);
xmlhttp.open("POST",serviceName,true);
xmlhttp.send(null);
}
無論我提出什麼地址,我得到的消息
'建立連接'
'準備好'
'失敗:,狀態:0'。
我已經嘗試過使用Firefox和Chrome,嘗試在本地運行它並託管,似乎沒有任何工作。我總是得到4個狀態爲0的ready狀態。
任何想法?我嘗試過多個網站連接,每次都是一樣的。
也嘗試過使用GET,設置一些標題,結果相同。幾乎嘗試了我能找到的每個示例,但沒有運氣
哦,男人,我是_sure_我嘗試了一個本地文件。剛剛重新嘗試一下,工作得很好。謝謝 :)。查看你的評論作爲答案,以便我可以將其標記爲 – Rob 2011-04-07 23:20:13
完成!通常的解決方法是服務器端代理,它爲您提取內容並將其提供給本地上下文中的JS。 – 2011-04-07 23:21:37
我認爲你打了跨域保護。你不能使用XHR與其他域進行交談 – Raynos 2011-04-07 23:22:18