Problem with getting AJAX to work [closed]
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this questionHaving some problems with getting some ajax script to work. Using this as the html page:
<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>
The script:
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);
}
No matter what address I put in, I get the messages
'connection established' 'ready' 'Failed: , status: 0'.I've tried on firefox and chrome, have tried running it locally and hosted, nothing seems to work. I always get a readyState of 4 and status of 0.
Any ideas? I've tried multiple sites to connect to, same thing every time. Have also tried with GET, setting some headers, same result. Pretty much tried every example I could find, with no luckYou seem to be trying to make an Ajax request to a remote location, which is impossible due to the Same Origin Policy.
Not sure whether there's a fix: If you control the remote location, you can make it send JSONP instead. If that's not possible, you're going to have to use a server-side proxy.
精彩评论