PDA

View Full Version : AJAX works locally but not on server



rbenditt
Jul 4th, 2007, 09:42 PM
I've put together a simple script that uses the XMLHttpRequest() function. It works when I run it locally, but when I upload it to my web hosting account, I never get a response back. Here is what I have:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html lang="en">
<head>

<script type="text/javascript" language="javascript">
var http_request = false;

function makeRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('GET', url + parameters, true);
alert(url);
http_request.setRequestHeader("User-Agent","XMLHttpRequest");
alert('ReadyState ='+http_request.readyState);
http_request.send(null);
alert('sent the request');
alert('ReadyState ='+http_request.readyState);
alert('Status ='+http_request.status);
}

function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('myspan').innerHTML = result;
} else {
alert('There was a problem connecting with the exchange rate server.');
}
}
}


</script>

</head>

<body>

<br><br>
<input type="button" name="button" value="GET get.php?test=2"
onclick="javascript:makeRequest('http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate', '?FromCurrency=GBP&ToCurrency=USD');">
<br><br>



<br><br>
Server-Response:<br>
<span name="myspan" id="myspan"></span>

</body>
</html>


I've thrown in a few alert boxes for debugging, so I could see a few things that are going on. The first "ReadyState" alert box shows different results depending on whether the script is being run locally or remotely. If it is running locally, I get a ReadyState of 1. When uploaded to my hosting account, I get ReadyState=0.

Any ideas about what may be going on here?

Thanks!

Ron

rwedge
Jul 5th, 2007, 12:15 AM
Does your web hosting account have the domain name of 'webservicex.net' or is www.webservicex.net a remote site?
If remote, your request will not succeed due to security.

rbenditt
Jul 5th, 2007, 03:49 AM
No, my web hosting account is not 'webservicex.net'. I'm just getting data from that server. ...or at least trying to. Is there a way to get this to work?

rbenditt
Jul 6th, 2007, 06:41 AM
Ok, I figured it out. It was a cross-domain scripting problem. I didn't realize that AJAX can't (easily) pass data from a third party through your website, and then off to an end user. I have set up a proxy application and now everything is good. Thanks for the help, it clued me into the fact that I couldn't use a third-party domain.