简体   繁体   中英

AJAX request to Google Maps API stopped working

I am trying to use Google Maps API in my application. I am not able to get result out of this simple API request. A week ago this code was working perfectly. I don't know if there is a problem in the Google's service or my code.

Here is my request:

       $.ajax({
            type: "GET",
            url: apilink,
            data: {
                origin: start.replace(/ /g, '+'),
                destination: end.replace(/ /g, '+'),
                waypoints: 'optimize:true|' + waypts.join('|').replace(/ /g, '+'),
                travelMode: google.maps.DirectionsTravelMode.DRIVING,
                sensor: false
            },
            dataType: "json"
        }).done(function (response) {
        alert("inside done");
       });

Looking into my javascript console, this was my request :

https://maps.googleapis.com/maps/api/directions/json?&origin=Chennai,+Tamil+Nadu,+India&destination=New+Delhi,+Delhi,+India&waypoints=optimize:false|New+Delhi,+Delhi,+India&travelMode=DRIVING&sensor=false

Now as I open this link, I am able to see the JSON response. But I am not able to get the alert "inside done".

What has gone wrong in just few days ?

done() is equivalent to the old success() call - this gets called only when the call succeeds. If the call fails, done callbacks will not be excuted. Always add a fail callback to ajax calls. If for nothing else, then just to remind yourself that this call can fail someday and the business rules don't yet specify what has to be done in that case.

As for your particular case, you may have hit your daily limit. It has happened to me before. Google's restrictions on the free usage are very generous when it comes to personal uses but are crippling if you are just scraping driving directions :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM