Detta inlägg är gammalt och kan innehålla inaktuell information.

Issue with realtimedepartures api

I am facing issue with realtimedepartuers api, when I directly visit the link, I receive the JSON response, but when the API is hit using jquery code and I try to access it from localhost, there is no response received.
I checked on api.sl.se, there is no crossdomain policy file available. Below is a sample URL which I am trying to use:

http://api.sl.se/api2/realtimedepartures.json?k...

Could someone suggest how I can make this API call using jQuery.

Kommentarer

  • WIth resrobot api´s you can use the parameter jsonpCallback=? to come around Same-origin Policy. (https://developer.mozilla.org/en-US/docs/Web/Se...)
    *But its not recommended * due to the fact your api key will be exposed in every request.

    $(document).ready(function(){

    //Example using $.getJSON from Jquery
    var jqxhr = $.getJSON( "https://api.resrobot.se/departureBoard?key= [key]&id=7400001&format=json&jsonpCallback=?",

    function(json) {
    console.log( json);
    })
    })

    How ever SL api dont allow jsonp calls so you must use server side code like node.js, php and java servlet to mention some.

    Starting tutorial for node.js.
    http://www.tutorialspoint.com/nodejs/

    1. client side code (javascript/jquery) call to your server
    2. server call the api
    3. server get the resonse and send it back to the client
    4. client present the answer
    Team Trafiklab
  • Thanks for your response. We are developing a kiosk application and there is no backend involved. We need this to work on client side itself. Is there a way to enable this only using JavaScript.

  • I understand the issue now, resrobot is a different api which will allow the jsonpCallback. Could you please tell me from where I can get all the id values. For sl api, I was searching on their home page and then checking the values from url. Please share the list or a link, which lists all possible values for the id.

    Thx

  • Hi Chetan

    Sorry but we dont have a list for SL stops.

    Instead you should use sl-platsuppslag. Documentation can be found here:
    https://www.trafiklab.se/api/sl-platsuppslag/sl...

    With SL-platsuppslag you can search for stop name and get back a list with stops names and ids. With the stop id from SL-platsuppslag you can use SL Reseplanerare 2 or SL Realtidsinformation 3.

    Best regards
    Daniel

    Team Trafiklab
  • Hello Daniel
    I have tried using resrobot api and I am getting an error. Below is the error:
    XMLHttpRequest cannot load https://api.resrobot.se/v2/departureBoard.json?key=[key]&id=740064073&maxJourneys=20. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

    Here is my sample code
    <script type="text/javascript">

    $(document).ready(function(){

    //Example using $.getJSON from Jquery
    var jqxhr = $.getJSON( "https://api.resrobot.se/v2/departureBoard.json?key=[key]&id=740064073&maxJourneys=20",

    function(json) {
    console.log( json);
    });
    });

    </script>

    Could you please suggest what is wrong with the call above.

  • If you are using $.getJSON you have to add the parameter jsonpCallback=? and it must be a questionmark

    $(document).ready(function(){

    //Example using $.getJSON from Jquery
    var jqxhr = $.getJSON( "https://api.resrobot.se/v2/departureBoard.json?key=[key]&id=740064073&maxJourneys=20&jsonpCallback=?",

    function(json) {
    console.log( json);
    });
    });

Kommentera eller skriv ett nytt inlägg

Ditt namn och inlägg kan ses av alla. Din e-post visas aldrig publikt.