logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Login


Options
View
Go to last post Go to first unread
Andrew Zich  
#1 Posted : Wednesday, August 24, 2011 3:50:49 PM(UTC)
Andrew Zich

Rank: Newbie

Groups: Registered
Joined: 8/24/2011(UTC)
Posts: 2
Location: Urbana, IL

Thanks: 1 times
It would be useful for web applications if a JavaScript / JSONP API was provided. The current JSON API provides all of the necessary information but is inaccessible by most web applications due to the cross-domain restrictions of XHR/AJAX. JSONP (JSONP @ Wikipedia) uses a script tag with an src (which can be from any domain) to send a GET request and the response executes a specified JavaScript function with the appropriate data. This should be a very simple extension on top of the existing JSON API. All that needs to be done is modify the JSON API code to accept an additional parameter and print it along with a few extra characters back around the JSON response. (It could even be in the same namespace where the existence of this parameter decides which method should be used)

Example:

GET mywebapp.html

Code:

...
<script type="text/javascript">
function gotStops(result) {
  //Do work
}
</script>
<script type="text/javascript" src="http://developer.cumtd.com/api/v1.0/json/stops.getList?key=0123456789abcdef0123456789abcdef&callback=gotStops">
...


GET http://developer.cumtd.c...ef&callback=gotStops

Code:

gotStops({"stat":"ok","stops":[{"code":"MTD5437","stop_id":"150DALE:1","stop_lat":40.114511,"stop_lon":-88.180673,"stop_name":"U.S. 150 & Dale"}, ... ]});


The only addition is the "gotStops();" wrapper where gotStops is specified as the callback parameter.

The real value in JSONP is the ability to dynamically generate a request within the browser based on user input such as in the following example:

GET mywebapp.html

Code:

...
<script type="text/javascript">
var stop = prompt("Please enter your stop ID:","GRNGRGST:1");

var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://developer.cumtd.com/api/v1.0/json/stop_times.getListByStop?key=0123456789abcdef0123456789abcdef&stop_id="+stop+"&callback=gotStopList";
head.appendChild(script);

function gotStopList(results) {
  //Present to user
}
</script>
...


As before, the response from the API would be a small wrapper around the JSON response; something along the lines of:

Code:

gotStopList({"stat":"ok","stop_times":[{"arrival_time":"06:08:18","departure_time":"06:08:18","stop_id":"WSFLDSRDN:4","stop_sequence":55,"trip":{"route_id":"GREEN","service_id":"GN1","shape_id":"5WIUE->","trip_headsign":"To WEST - HOLIDAY PARK","trip_id":"3439__GN1"}},...]})


Final Thoughts:

  • The name of the callback is important as it allows the web app to know the context of the request. If two separate stops.getListByLatLon requests were made at the same time and both used the same callback function, the web app wouldn't necessarily know which was which.
  • The rate limiting would have to be reevaluated. Every instance of the webapp would have the same API key so limiting should probably be done on a per IP or per IP per API key basis.
  • It would be even more useful if a nice, encapsulated JavaScript library handled all of the interpretation of the data like with the Google Maps JavaScript API but that can always be done later.


Thanks for reading,
Andrew Zich
Sean  
#2 Posted : Thursday, August 25, 2011 2:09:22 PM(UTC)
Sean

Rank: Administration

Groups: Administrators
Joined: 4/11/2011(UTC)
Posts: 42

Was thanked: 3 time(s) in 3 post(s)
Done. It's not in the documentation yet, but you can add the "callback" query string parameter to get it working. Happy coding!
thanks 1 user thanked Sean for this useful post.
Andrew Zich on 8/25/2011(UTC)
Andrew Zich  
#3 Posted : Thursday, August 25, 2011 2:25:20 PM(UTC)
Andrew Zich

Rank: Newbie

Groups: Registered
Joined: 8/24/2011(UTC)
Posts: 2
Location: Urbana, IL

Thanks: 1 times
Looks like this will be perfect for my needs.

Thank you,
Andrew Zich
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.