Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:

    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('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

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •