admin管理员组

文章数量:1391991

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 = ".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>

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..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>
Share Improve this question edited Jun 7, 2010 at 12:22 Taiba asked Jun 7, 2010 at 11:34 TaibaTaiba 131 silver badge4 bronze badges 1
  • I think we need a bit of clarification: Does it really work in IE, but not Firefox (or the other way around)? Is "drc.edeliver..au" external? – Chris Lercher Commented Jun 7, 2010 at 12:04
Add a ment  | 

6 Answers 6

Reset to default 4

Is your page hosted at http://drc.edeliver..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.

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


UPDATE: Further to the ments, 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..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..au/ratecalc.asp, while it appears to the browser as if it is being served from the same origin.

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 remendation 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!

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.

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

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);

本文标签: javascriptAJAX ProblemNo response text in FireFoxbut ok in IEStack Overflow