468,121 Members | 1,464 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,121 developers. It's quick & easy.

AJAX works locally but not on server

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:


[html]
<!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>
[/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
Jul 4 '07 #1
10 2212
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Could you provide a link to a test page?
Jul 5 '07 #2
Sure. Here's my test page:

http://www.tappingware.com/GetCurrencyRate.html
Jul 5 '07 #3
Ok, I found the issue. It was a cross-domain issue. I didn't know that you can (easily) use AJAX to get data from a third-party source. I set up an application proxy to relay the data, and now everything is working. Thanks!
Jul 6 '07 #4
acoder
16,027 Expert Mod 8TB
Glad you got it working (before I had a chance to look at it!)
Jul 6 '07 #5
briana
3
Ok, I found the issue. It was a cross-domain issue. I didn't know that you can (easily) use AJAX to get data from a third-party source. I set up an application proxy to relay the data, and now everything is working. Thanks!
I'm new at all this and I have been trying to solve this problem for a few days now!
Locally (windows running apache) all is perfect, but on my hosted server (a linux box running apache) the readyState only makes it to 2 (sent).

How did you "set up an application proxy to relay the data"? Sounds like this is something that will solve my problem, but don't quite know what it is or how to instantiate it. Thanks for any help.
Oct 4 '07 #6
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!
How did you "set up an application proxy to relay the data"? Sounds like this is something that will solve my problem, but don't quite know what it is or how to instantiate it. Thanks for any help.
The application proxy is to avoid cross-domain issues. The data is passed to a server-side script which deals with getting the results from a different domain.
Oct 4 '07 #7
briana
3
Welcome to TSDN!

The application proxy is to avoid cross-domain issues. The data is passed to a server-side script which deals with getting the results from a different domain.
I talked to our IT guy and he said that when the ajax call is used Apache (on Linux) is segfaulting. My Apache server (on Windows) doesn't have this problem at all - he's compared our server config files and they are the same.

Has anyone heard of this happening while using asynchronous calls?
thanks for any help.
Oct 23 '07 #8
acoder
16,027 Expert Mod 8TB
I talked to our IT guy and he said that when the ajax call is used Apache (on Linux) is segfaulting. My Apache server (on Windows) doesn't have this problem at all - he's compared our server config files and they are the same.

Has anyone heard of this happening while using asynchronous calls?
thanks for any help.
Can you post some of your code? Can you access the server-side page without Ajax and does it produce the required output?
Oct 23 '07 #9
briana
3
Can you post some of your code? Can you access the server-side page without Ajax and does it produce the required output?

You aren't gonna believe this! It all had to do with how the directory was being written! I'm on windows testing but production server is on linux....I use backslashes for the directory structure and it wanted forward slashes...Plus, I was using php to echo javascript and the file path was getting lost in translation!

Thanks for the speedy responses and bearing with me! Hope this helps someone else at some point.
Oct 23 '07 #10
acoder
16,027 Expert Mod 8TB
You aren't gonna believe this! It all had to do with how the directory was being written! I'm on windows testing but production server is on linux....I use backslashes for the directory structure and it wanted forward slashes...Plus, I was using php to echo javascript and the file path was getting lost in translation!

Thanks for the speedy responses and bearing with me! Hope this helps someone else at some point.
Glad to hear that you got it working and thanks for posting the solution.

Post again anytime if you have more questions.
Oct 24 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by nick | last post: by
9 posts views Thread by schmeckel | last post: by
13 posts views Thread by adam | last post: by
3 posts views Thread by Rob Meade | last post: by
8 posts views Thread by rbrowning1958 | last post: by
18 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.