Chris Dutrow
Chris Dutrow

Reputation: 50362

Google Maps API - Can't use Ajax? Even with json?

I was trying to use the google maps API within some jQuery ajax, however it looks like I can't do this because it's cross-domain, even though I'm using json?

Why is this? What should I do to resolve this?

$.ajax({
    type: "GET",
    url: 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=405+Lexington+Avenue+Manhattan+NY',
    error: function(request, status) {
        alert('fail');
    },
    success: function(data, textStatus, jqXHR) {
        alert('success');
    }
});

Thanks!

Upvotes: 1

Views: 1098

Answers (2)

Nikita Rybak
Nikita Rybak

Reputation: 68006

Returning json content doesn't exclude you from cross-domain restrictions. (You might be confusing json and jsonp here.)

You can use script, as another person noted, but I would recommend switching to javascript api instead. This one is probably designed to be used in server-side code.

Upvotes: 2

Ryan Olds
Ryan Olds

Reputation: 4847

That's right, cross domain ajax calls are not allowed. Instead create a "script" element on the page with the src set to that URL.

You can also use $.getScript() - http://api.jquery.com/jQuery.getScript/

Upvotes: 1

Related Questions