views:

93

answers:

5

Hi,

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&amp;Destination_Postcode=6055&amp;Country=AU&amp;Weight=100&amp;Service_Type=STANDARD&amp;Length=100&amp;Width=100&amp;Height=100&amp;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>
+2  A: 

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
+1  A: 

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

http://blogs.msdn.com/b/ieinternals/archive/2010/04/06/comet-streaming-in-internet-explorer-with-xmlhttprequest-and-xdomainrequest.aspx

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!

Chris Lercher
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
@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
It works in IE7.Doesn't work in Firefox 3.6.3.
Taiba
What is the URL of your own page, @Taiba?
Pointy
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
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
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
@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
+3  A: 
Pointy
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
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
I'm just surprised I don't get a proper error message like I would if I used an iframe.
Taiba
I agree; I don't know why the error isn't made more obvious. It would certainly cut down on new Stackoverflow questions :-)
Pointy
A: 

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
@Taiba: 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
A: 

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
Furqan