NickF
NickF

Reputation: 169

Can retrieve data with $.getJSON but not $.ajax

I'm trying to retrieve data from the cloudmade routing API service. I can do it fine using $.getJSON with the following code:

<button id="getJSON">Get JSON</button>


$(function () {

    $("#getJSON").click(function () {

        $.getJSON("http://routes.cloudmade.com/c6f2762bfe00414f822a9dec443569f5/api/0.3/51.500,0.100,51.500,0.1001/car.js?callback=?", function (data) {
            var test = data.route_geometry;
            alert(test);
        });
    });
});

Also see http://jsfiddle.net/V3qgZ/72/

When I try to do the same request using $.ajax, I cannot retrieve the data, but I'm not sure where my error is. The reason I want to use $.ajax is because $.getJSON is asynchronous, where as $.ajax can be set to 'async: false'. The code for the $.ajax request is here:

<button id="getJSON">Get JSON</button>


$(function () {
$("#getJSON").click(function () {
    var url = "http://routes.cloudmade.com/c6f2762bfe00414f822a9dec443569f5/api/0.3/51.500,0.100,51.500,0.1001/car.js";
    $.ajax({
        async: false;
        dataType: "jsonp",
        url: url,
        success: function (data) {
            test = data.route_geometry;
            alert(test);
        }
    });
});
});

Also at http://jsfiddle.net/FhfVW/1/

Any help much appreciated!

Upvotes: 1

Views: 756

Answers (1)

Jaya Mayu
Jaya Mayu

Reputation: 17257

There is a syntax error async: false remove ; and add ,

Here is a working fiddle http://jsfiddle.net/mayooresan/FhfVW/2/

Upvotes: 3

Related Questions