0

I am working on an Ionic (v1) app, I am doing a simple call to this endpoint:

https://maps.googleapis.com/maps/api/directions/json?origin=25.6909532,-100.3705846&destination=25.6909532,-100.3705846&waypoints=optimize:true|25.6507548,-100.3424051|25.6546534,-100.356101|25.63622,-100.3124573

Which works correctly if you simply open it on a browser, it returns well formed JSON (I can copy it and parse it on a jsonviewer).

But if I call it using $http's jsonp method (I use jsonp because otherwise it will cause a CORS error), it will cause this error:

Uncaught SyntaxError: Unexpected token :

This is my code (simplified):

$http.jsonp('https://maps.googleapis.com/maps/api/directions/json?origin=25.6909532,-100.3705846&destination=25.6909532,-100.3705846&waypoints=optimize:true|25.6507548,-100.3424051|25.6546534,-100.356101|25.63622,-100.3124573', {jsonpCallbackParam: 'callback'})
.then(function(response){
    console.log(response);
});

Also I see that the browser is getting the json response correctly: enter image description here

Multitut
  • 2,089
  • 7
  • 39
  • 63
  • jsonp expects a json structure wrapped in an function: JSON_CALLBACK ({ /* JSON */ }) Your url returns just a plain json object. – Stef Chäser Mar 07 '18 at 05:35
  • Thanks, that's going to be the problem. The thing is that if I don't use jsonp, it gives me a CORS related error when I call. – Multitut Mar 07 '18 at 13:39
  • Yes, I think you have to use google's javascript library: https://stackoverflow.com/questions/29834185/how-do-i-get-json-from-google-directions-api-using-jquery – Stef Chäser Mar 07 '18 at 20:42

0 Answers0