View Full Version : AJAX works locally but not on server

Jul 4th, 2007, 10: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">

<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
} 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('ReadyState ='+http_request.readyState);
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) {
result = http_request.responseText;
document.getElementById('myspan').innerHTML = result;
} else {
alert('There was a problem connecting with the exchange rate server.');




<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');">

<span name="myspan" id="myspan"></span>


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?



Jul 5th, 2007, 01: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.

Jul 5th, 2007, 04: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?

Jul 6th, 2007, 07: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.