alyx
alyx

Reputation: 2733

JSON Request Help

First off, I would be doing this with jQuery if the Google Places API would allow JSONP requests, but instead I am using a standard XMLHttpRequest:

function load() {
    var req = new XMLHttpRequest();
    req.open('GET', 'https://maps.googleapis.com/maps/api/place/details/json?reference=CnRhAAAARMUGgu2CeASdhvnbS40Y5y5wwMIqXKfL-n90TSsPvtkdYinuMQfA2gZTjFGuQ85AMx8HTV7axABS7XQgFKyzudGd7JgAeY0iFAUsG5Up64R5LviFkKMMAc2yhrZ1lTh9GqcYCOhfk2b7k8RPGAaPxBIQDRhqoKjsWjPJhSb_6u2tIxoUsGJsEjYhdRiKIo6eow2CQFw5W58&sensor=true&key=xxxxxxxxxxxxx', false);
    req.send(null);

    if (req.status == 200) {
      dump(req.responseText);
    }
}

I am having problems with cross-domain origin security stuff, but what I am really asking is if this is the easiest way to go about requesting JSON from the Google Places API?

I am looking for the easiest way to do this without having to setup a local proxy to deal with cross domain origin stuff.

Or is there another javascript toolkit that uses regular JSON requests?

Upvotes: 0

Views: 231

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

I would be doing this with jQuery if the Google Places API would allow JSONP requests. is there another javascript toolkit that uses regular JSON requests?

jQuery is not limited to JSONP.

function load() {
    var url = 'https://maps.googleapis.com/maps/api/place/details/json?reference=CnRhAAAARMUGgu2CeASdhvnbS40Y5y5wwMIqXKfL-n90TSsPvtkdYinuMQfA2gZTjFGuQ85AMx8HTV7axABS7XQgFKyzudGd7JgAeY0iFAUsG5Up64R5LviFkKMMAc2yhrZ1lTh9GqcYCOhfk2b7k8RPGAaPxBIQDRhqoKjsWjPJhSb_6u2tIxoUsGJsEjYhdRiKIo6eow2CQFw5W58&sensor=true&key=xxxxxxxxxxxxx';
    $.ajax(url, {
       async:   false,
       success: function(data, textStatus, jqXHR) {
           dump(data);
       }
    });
}

Note that:

cross-domain requests and dataType: "jsonp" requests do not support synchronous operation

so you may have to switch to asynchronous requests.

Upvotes: 1

Related Questions