Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AJAX works locally but not on server

Status
Not open for further replies.

rbenditt

Programmer
Jul 4, 2007
3
US
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:

Code:
<!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('[URL unfurl="true"]http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate',[/URL] '?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
 
No matter where the page is hosted, you always run it locally, on your browser.

Maybe it has to do with your security settings and you're not allowing ActiveX controls from the Internet. I'd try adding your hosting to your trusted sites.

Cheers,
Dian
 
Are both the page calling the AJAX and the page you call on the server, or is only one hosted on the server? They both need to be in the same place (on the same domain).

Dana



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Even if the site is on your trusted sites (for IE) or signed (for Mozilla)?

Cheers,
Dian
 
I've added my domain and the webservicex.net domain to my trusted sites list, and I still get an "access denied" error.

Basically what I want to do is to get currency exchange rates and have them available for my users. The webservicex.net site has a great web service for this... but I'm really not sure how to use it! I mean, I can get it to work in my browser if I run it locally, but when I publish my web page, it all falls apart.
 
Some ideas:
[link]http://www.xml.com/pub/a/2005/11/09/fixing-ajax-xmlhttprequest-considered-harmful.html[/url]
[link]http://premshree.livejournal.com/66129.html[/url]

Cheers,
Dian
 
There are two things you need to do in this occasion for moz-based. One on the client's security setting and one on the page design asking for permission.

[1] On the page, you add this before (position is not absolute, as long as it is before) your open statement.
[tt]
http_request.onreadystatechange = alertContents;
[blue]if (window.XMLHttpRequest) {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead")
}[/blue]
http_request.open('GET', url + parameters, true);
alert(url);
[/tt]
[2] But in order the users are offered a choice, they must be willing to take. (If they don't want to, they are not your target audience.) This choice is the security setting of the "signed.applets.codebase_principal_support" and it must be set to read:
[tt]
Preference Name: signed.applets.codebase_principal_support
status: user set
type: boolean
value: true
[/tt]
If you/they don't understand [2], forget about it. Do your part on [1] see if you succeed in offering a choice.
 
Thanks guys! That was totally my problem. Those links about cross-domain AJAX really set me straight. I have managed to set up php page to act as a proxy and now everything works. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top