0

It was really strange because i have this code for months working fine but since the last week i can't use it anymore. The code was always the same:

   $.getJSON("http://maps.googleapis.com/maps/api/distancematrix/json?origins="+origin+"&destinations="+destinations+"&mode=walking&language=es-ES&sensor=false", function(datos) {
            alert("hello");
                    // code
            }
        });

I can not even see the "hello" alert. But i can see the json code, this is an example: Example

So, which may be the problem?

F4rmer
  • 45
  • 1
  • 6
  • Perhaps you should check for returned errors. http://stackoverflow.com/questions/1740218/error-handling-in-getjson-calls – geocodezip Nov 25 '13 at 22:53
  • Thanks, but i can't catch the error description... If i use ".fail(function(jqXHR, textStatus, errorThrown) { alert('getJSON request failed! ' + textStatus); })" the "textStatus" just shows the word "error". And if i try to catch it with "$.ajax({ url: 'url', dataType: 'json', error: function( data ) { alert( "ERROR: " + data ); } });" the "data" just shows [Object object], which is very common in these cases. – F4rmer Nov 25 '13 at 23:24
  • the "errorThrown" alert "NetworkError: A network error occurred." – F4rmer Nov 25 '13 at 23:46

1 Answers1

3

It's fine for you when it works for months, but your mistake is to rely on a fact that you can't rely on.

You are requesting the DistanceMatrix-Webservice via Javascript, but you've ignored this note in the documentation:

Looking to use this service in a JavaScript application? Check out the DistanceMatrixService class of the Google Maps API v3.

The Webservice doesn't support JSONP, and he currently didn't send an appropriate Access-Control-Allow-Origin-header(that's the fact you did rely on and that is required to access this Service via AJAX, but the documentation didn't say that this header will be sent), so this Service can't be requested reliable via AJAX/Javascript.

In google-chrome the console e.g. will tell you this:
XMLHttpRequest cannot load [URL]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '[URL]' is therefore not allowed access.

Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • Thanks! But, what do you recommend me to use? The Distance Matrix javascript function doesn't work for me (it didn't recognize some countries and places to calculate the distance and i need to fix it). Probably i should use the service in php, but it maybe have the same problem. I don't know other way to use the service – F4rmer Nov 26 '13 at 00:36
  • What should I recommend? When the services didn't give you the desired result you must look for other service-providers – Dr.Molle Nov 26 '13 at 00:40
  • 1
    The webservice and the [Google Maps Javascript API v3 Directions Matrix service](https://developers.google.com/maps/documentation/javascript/distancematrix) should return the same results (they use the same data), if the javascript service doesn't work because of data issues, the web service probably won't work either. – geocodezip Nov 26 '13 at 02:57