简体   繁体   中英

Retrieving json data from external server

I asked a similar question to this earlier but perhaps this is a bit more clear.

I am trying to retrieve json data from the OSRM routing server (API here: https://github.com/DennisOSRM/Project-OSRM/wiki/Server-api ).

I am currently using this code:

http://jsfiddle.net/FhfVW/10/

$(function () {
$("#getJSON").click(function () {
    var url = "http://router.project-osrm.org/viaroute?loc=51.500,0.100&loc=51.500,0.1001&jsonp=myroute";
    $.ajax({
        dataType: "json",
        url: url,
        jsonpCallback: 'myroute',
        success: function (data) {
            test = data.route_geometry;
            alert(test);
        }
    });
});
});

However I don't get any response from this. Eveidently something is wrong, although I've tried it removing the callbacks and 'jsonp=myroute' part with no success either.

Thanks for any help.

Nick

Finally, i got this working

$(function () {
    var url = "http://router.project-osrm.org/viaroute?loc=51.500,0.100&loc=51.500,0.1001";
    $.ajax(url, {dataType:"jsonp", jsonp:"jsonp", cache:true}).success(function() {
       console.log(arguments);
    });
});

if you put the url into the browser, which gets generated by jquery, then you see a 400 error, as jquery adds the "& =[TIMESTAMP]" param. But your remote server says, the url is then malformed. If you give jquery the "cache" option with a true value, it will not append the " " parameter.

If you have further issues, try to replace the "success" with the "always" callback, as you will then be notified every time the callback succeds or not. Because sometimes there is an error within jquery, which you wouldnt notice in developer tools

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