问题
This is inside a Drupal site. I sent an Ajax request using GDownloadUrl
using the following code. I got this GDownloadurl
method from the Google Maps JavaScript API version 2:
GDownloadUrl(down_load_url, function(data) {
var xml = GXml.parse(data);
});
This down_load_url
has the request and the data variable will get the response as an XML.
However, in Chrome this data variable contains nothing. In other browsers this function gets the requested data correctly. What is the wrong with Chrome?
回答1:
This gentleman has an example of using GDownloadUrl at: http://googlemapsapi.blogspot.com/2007/02/gdownloadurl-update-better-error.html
You might try adding the responseCode parameter to your callback function to see if Chrome is erroring for some reason. This example works in my version of Chrome, so make sure you are running the latest version.
Here is his code example:
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
// Download the data in data.xml and load it on the map. The format we
// expect is:
// <markers>
// <marker lat="37.441" lng="-122.141"/>
// <marker lat="37.322" lng="-121.213"/>
// </markers>
GDownloadUrl("data.xml", function(data, responseCode) {
// To ensure against HTTP errors that result in null or bad data,
// always check status code is equal to 200 before processing the data
if(responseCode == 200) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
map.addOverlay(new GMarker(point));
}
} else if(responseCode == -1) {
alert("Data request timed out. Please try later.");
} else {
alert("Request resulted in error. Check XML file is retrievable.");
}
});
}
}
来源:https://stackoverflow.com/questions/7147079/gdownloadurl-is-not-working-on-chrome