1

I am making a simple AJAX call to an external site. It works ok in IE, but in Firefox, not response text is returned.

I think it might have something to do with the response being "chunked", but I'm not sure.

Any ideas? Thanks.

<html>
<head>
    <script type="text/javascript" charset="utf-8">
        function loadXMLDoc() {
            var xmlhttp;
            var urlString = "http://drc.edeliver.com.au/ratecalc.asp?Pickup_Postcode=6025&Destination_Postcode=6055&Country=AU&Weight=100&Service_Type=STANDARD&Length=100&Width=100&Height=100&Quantity=2";
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            } else {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4) {
                    window.alert(xmlhttp.responseText);
                }
            }
            xmlhttp.open("GET", urlString, true);
            xmlhttp.send();
        }
    </script>
</head>
<body>
    <span onclick="loadXMLDoc()">Click Me</span>
</body>
</html>
Taiba
  • 13
  • 1
  • 4
  • I think we need a bit of clarification: Does it really work in IE, but not Firefox (or the other way around)? Is "http://drc.edeliver.com.au" external? – Chris Lercher Jun 07 '10 at 12:04

6 Answers6

4

Is your page hosted at http://drc.edeliver.com.au also? If not, then you can't make an XMLHttpRequest to that URL. It's a violation of basic browser security which, for your IE tests, are probably suppressed by explicit browser configuration.

edit — I think IE lets the "local security zone" (or whatever they call it) get away with stuff that is not allowed for "Internet" zone pages.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • seriously? Ooo - that would explain a lot. That would suck too - I was hoping to use the service without having to resort to server-side scripting. – Taiba Jun 07 '10 at 12:25
  • Ok - so is this the answer? Can't use AJAX for cross-domain services (easily)? Use a server-side script instead? I guess I should have known - I just never came across it because I've never tried to call cross-domain before. – Taiba Jun 07 '10 at 13:44
  • I'm just surprised I don't get a proper error message like I would if I used an iframe. – Taiba Jun 07 '10 at 13:47
  • I agree; I don't know why the error isn't made more obvious. It would certainly cut down on new Stackoverflow questions :-) – Pointy Jun 07 '10 at 14:18
2

You may want to try using a relative URL in urlString, to avoid problems with the Same Origin Policy.


UPDATE: Further to the comments, if JSONP is not an option, you could also set up a simple reverse proxy to get around the same origin policy. You could use mod_proxy if you are using Apache. This would allow you to use relative paths in your AJAX request, while the HTTP server would be acting as a proxy to any "remote" location.

The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. You would typically use it as follows:

ProxyPass     /remote/     http://drc.edeliver.com.au/

In this case, the browser would be able to request /remote/ratecalc.asp but the server would serve this by acting as a proxy to http://drc.edeliver.com.au/ratecalc.asp, while it appears to the browser as if it is being served from the same origin.

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
1

Yes, the chunked response (COMET streaming) may be the problem:

Link

As streaming is still associated with many problems (e.g. when using proxies), my current recommendation would be to use long polling instead (closing the response for each chunk, and issuing a new request immediately). It's an unfortunate situation - I'd prefer to use streaming, too.

Edit

We found out, that this isn't actually the problem here!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • According to that page, it should work in Firefox and not IE, which is the opposite of the situation reported here ... Also, note: "AJAX call to an **external site**" – Pointy Jun 07 '10 at 11:53
  • @Pointy: Yes, it has worked on Firefox in my tests that I did some time ago, so I assume that it's a typo in the question (?) – Chris Lercher Jun 07 '10 at 11:58
  • What is the URL of your own page, @Taiba? – Pointy Jun 07 '10 at 12:14
  • Also - it is an external site I am connecting to. I have no control over the response. If anyone has an example code snippet to make this work - I'd appreciate it. – Taiba Jun 07 '10 at 12:16
  • I am just trying to get it working from a simple static HTML page at the moment. It is for a friend's web page and they want to use this external service to calculate shipping costs. – Taiba Jun 07 '10 at 12:21
  • I have edited the original post to include the sample HTML. Run it in IE and it works. Run it in Firefox and it doesn't. – Taiba Jun 07 '10 at 12:23
  • @Taiba: If the service has an option to return the values in JSON format, you could use JSONP to work around SOP: http://www.ibm.com/developerworks/library/wa-aj-jsonp1/ – Chris Lercher Jun 07 '10 at 12:35
0

I believe the problem is a simple case of cross-domain security.

I am trying to make an AJAX call from a local static HTML page to an external URL.

IE allows me to do this. Firefox does not allow me to do this.

Firefox does not provide an error message (like it does with a cross-domain iFrame DOM access). The response text is simply empty.

Thanks everyone for your help. Sorry to post such a dozey question.

Taiba
  • 13
  • 1
  • 4
  • Yes, an empty responseText is normal when you are blocked by the same origin policy. Check out this SO post: http://stackoverflow.com/questions/1941340/ for example. You may want to accept one of the answers if any one was helpful towards solving your problem. You can accept an answer by ticking on the green tick on the left of each... And welcome to Stack Overflow :) – Daniel Vassallo Jun 07 '10 at 15:13
0

For cross domain request, have a look at this article.

Probably you will need to enable following headers for it to work in FF/Chrome:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
sth
  • 222,467
  • 53
  • 283
  • 367
Furqan Hameedi
  • 4,372
  • 3
  • 27
  • 34
0

I Also faced same issue with ajax using XMLHttpRequest, below is the code which helped me

var link=document.createElement('a'); 
link.href= url; 
document.body.appendChild(link); 
link.click();  
setTimeout(function()
{ 
     document.body.removeChild(link);
     window.URL.revokeObjectURL(data); 
}, 100);