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