Last active
December 18, 2015 03:39
-
-
Save ElectricMaxxx/5719717 to your computer and use it in GitHub Desktop.
Problem with an ajax request $.ajax() in some IE.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hi, | |
here is the code of the main function: | |
``` | |
function doActualPointRequest(adress,fn){ | |
var url = "http://maps.googleapis.com/maps/api/geocode/json?address="+adress+"&sensor=false"; | |
if ($.browser.msie && window.XDomainRequest) { | |
// Use Microsoft XDR | |
var xdr = new XDomainRequest(); | |
xdr.open("get", url); | |
xdr.onload = function() { | |
// XDomainRequest doesn't provide responseXml, so if you need it: | |
var dom = new ActiveXObject("Microsoft.XMLDOM"); | |
dom.async = false; | |
dom.loadXML(xdr.responseText); | |
o = eval("("+xdr.responseText+")"); | |
if(o.status == "OK"){ | |
var GeoX = o.results[0].geometry.location.lng; | |
var GeoY = o.results[0].geometry.location.lat; | |
fn(GeoX,GeoY) | |
} | |
else{ | |
var empty = []; | |
fillHaendlerList(empty); | |
//console.log(o); | |
} | |
}; | |
xdr.send(); | |
} else { | |
jQuery.support.cors = true | |
$.ajax({ | |
url: url, | |
dataType: 'json', | |
error:function(xhr,textStatus,errorThrown) { | |
console.log("Unter der url: "+url+" konnte kein Ergebniss gefunden werden. Es kam folgender Fehler zurück: \n\ | |
"+textStatus+'\n'+errorThrown); | |
}, | |
success: function(o){ | |
if(o.status == "OK"){ | |
var GeoX = o.results[0].geometry.location.lng; | |
var GeoY = o.results[0].geometry.location.lat; | |
fn(GeoX,GeoY) | |
} | |
else{ | |
var empty = []; | |
fillHaendlerList(empty); | |
} | |
} | |
}); | |
} | |
} | |
``` | |
i forgot, that i solved the problem by adding a native ajax-reqeust if this is possible | |
The question was: why can`t the else part do it alone? | |
When i do only the else part, i got errors, but only sometime in some IE version. These errors arn`t errors from the response. I got the if part from a forum post, i don`t know where. It`s funny, that this part uses | |
`dom.loadXML();`. | |
The url is cool and works on its own, when i copy it into the browser (everytime) | |
ex: `http://maps.googleapis.com/maps/api/geocode/json?address=Deutschland+91522+Ansbach&sensor=false ` | |
Now a commented the if part and uses only the else part and get this error in IE8: | |
`` | |
Unter der url: http://maps.googleapis.com/maps/api/geocode/json?address=Deutschland+91522&sensor=false konnte kein Ergebniss gefunden werden. Es kam folgender Fehler zurück: | |
error | |
Error: Zugriff verweigert | |
`` | |
This is the line i put into the error callback. I don`t understand: `ERROR: Acces Denied` (translated it for you) | |
And this is the xhr object: | |
``` | |
{"readyState":0,"status":0,"statusText":"Error: Zugriff verweigert\r\n"} | |
``` | |
I put it now back to the first (the working one) state. You can find the application under: | |
http://www.pigrol.de/Startseite/Services/Haendlersuche | |
but: | |
- works only in german | |
- works only with german zip codes, cause i put Germany in front of the adress ;-) | |
This app works in two request: | |
one to get the coords, where the user is looking for a seller | |
the second to my backend (works fine with only $.ajax()) to get al list (json) of sellers that are in the near | |
and than show a list + card markers, ... | |
it works now whith that extra code, but why does it not without it inside of IE8 (i tried it some minutes ago) |
As you are probably aware the version 3 of Google's geocode API they took away the JSONP option which made for easy cross-domain requests. I see you are using CORS to try to get around this, but unfortunately support for that is tricky in older browsers http://caniuse.com/cors
It does seem that if you use Google's library wrapper that they have somehow gotten around the issue with older browsers. I'm not sure what they are doing under the covers, but I am guessing they are using some special DOM trick to get around the cross-browser issue http://stackoverflow.com/questions/4132685/google-maps-geocode-api-v3-not-returning-result-in-javascript-function
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
somewhere i missed the code-view thing ;-)