开发者

xmlHttp request status is 0 for google maps http geocoder

I am trying to submit an HTTP request via AJAX from the client to the Google Maps Geocoding service. I keep getting a status of 0.

I know the request is valid because when I enter the URL right into the browser address bar I get a valid result. Here is the code (assume 'url_string' has a valid url to the geocoding service - I have tested this already as mentioned above):

var request = GXmlHttp.create();
request.open("GET", url_string, true);
request.onreadystatechange = function() {
    if (request.readyState == 4) {
        alert("STATUS IS "+ request.status);  
    }
}
request.send(null);

My app is running on Google appengine and I get开发者_运维百科 the error when I try it locally but also when I deploy and try it.

Any help would be appreciated.


You can't submit your own XmlHttpRequest to Google as this violates the Same Origin Policy, and is thus being rejected by the browser. As bobince said, use Google's provided classes for doing it instead. They get around this via clever DOM hacks which you shouldn't need to reimplement.


0 is the status code you get for a completely failed connection, where there is no HTTP response to return a status code from. This can be eg. various socket errors (though IE will give you a WinSock error code like 12029 in this case).

when I enter the URL right into the browser address bar I get a valid result.

I suspect the URL isn't on your server. GXmlHttp, being a tiny wrapper around XMLHttpRequest, cannot do cross-domain requests.

Use the specific class Google give you for doing geocoding rather than trying to do it with a plain XMLHttpRequest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜