RTS REST API Code Examples
Javascript: JSON-P (JSON-with-Padding)
The code below handles the AJAX call using jQuery. By using the JSON-P data type, the script is able to overcome the cross-server scripting problem. Learn more about JSON-P.
<script type="text/javascript">
jQuery(document).ready(function () {
// Send an AJAX request
jQuery.ajax({
url: 'http://api.rgrta.com/stops?key=XXXXX&linedirid=98110',
type: 'GET',
dataType: 'jsonp',
success: function (data) {
// On success, 'data' contains a list of stops, which we can handle using the each method.
jQuery.each(data, function (key, val) {
// Add a table row with all the data in it
jQuery("#stops").find('tbody')
.append(jQuery('<tr>')
.append(jQuery('<td>')
.append(val.StopID)
)
.append(jQuery('<td>')
.append(val.StopName)
)
.append(jQuery('<td>')
.append(val.OnStreet)
)
.append(jQuery('<td>')
.append(val.AtStreet)
)
.append(jQuery('<td>')
.append(val.StopLat)
)
.append(jQuery('<td>')
.append(val.StopLon)
)
);
});
},
statusCode: {
403: function () {
alert("403 error.");
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Fail: " + textStatus + " - " + errorThrown);
}
});
});
</script>
The code below creates the table for rows to be added to on the page and initializes the table with the headers for the data.
<div>
<h1>Stops for Line Dir Id 98110</h1>
<table id="stops" style="width: 100%;">
<tbody>
<tr>
<th>Stop ID</th><th>Stop Name</th><th>On Street</th><th>At Street</th><th>Latitude</th><th>Longitude</th>
</tr>
</tbody>
</table>
</div>
Javascript: CORS (Cross-Origin Resource Sharing)
The code below handles the AJAX call using jQuery. By using CORS, the script is able to overcome the cross-server scripting problem. Implementing an AJAX call with CORS is identical to a call with JSON-P for the RTS API, except that you do not need to assign a dataType in the ajax call. For more information about CORS, click here.
<script type="text/javascript">
jQuery(document).ready(function () {
// Send an AJAX request
jQuery.ajax({
url: 'http://api.rgrta.com/stops?key=XXXXX&linedirid=98110',
type: 'GET',
success: function (data) {
// On success, 'data' contains a list of stops, which we can handle using the each method.
jQuery.each(data, function (key, val) {
// Add a table row with all the data in it
jQuery("#stops").find('tbody')
.append(jQuery('<tr>')
.append(jQuery('<td>')
.append(val.StopID)
)
.append(jQuery('<td>')
.append(val.StopName)
)
.append(jQuery('<td>')
.append(val.OnStreet)
)
.append(jQuery('<td>')
.append(val.AtStreet)
)
.append(jQuery('<td>')
.append(val.StopLat)
)
.append(jQuery('<td>')
.append(val.StopLon)
)
);
});
},
statusCode: {
403: function () {
alert("403 error.");
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Fail: " + textStatus + " - " + errorThrown);
}
});
});
</script>
The code below creates the table for rows to be added to on the page and initializes the table with the headers for the data.
<div>
<h1>Stops for Line Dir Id 98110</h1>
<table id="stops" style="width: 100%;">
<tbody>
<tr>
<th>Stop ID</th><th>Stop Name</th><th>On Street</th><th>At Street</th><th>Latitude</th><th>Longitude</th>
</tr>
</tbody>
</table>
</div>
C# ASP.NET
.cs file
public partial class Schedules : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ListRoutes();
}
}
public void ListRoutes()
{
string json = DownloadJsonData(@"http://api.rgrta.com/stops?key=XXXXX&linedirid=98110");
var jarray = JsonConvert.DeserializeObject<List<Stop>>(json);
repResults.DataSource = jarray;
repResults.DataBind();
}
private static string DownloadJsonData(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Referer = "http://www.yoursite.com";
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
return reader.ReadToEnd();
}
}
catch (Exception ex)
{
return "ERROR: " + ex.Message + " | " + ex.InnerException;
}
}
}
public class Stop
{
public int StopID { get; set; }
public string StopAbbr { get; set; }
public string StopName { get; set; }
public string NodeAbbr { get; set; }
public string OnStreet { get; set; }
public string AtStreet { get; set; }
public string StopLat { get; set; }
public string StopLon { get; set; }
public int AgencyId { get; set; }
}
.aspx file
<asp:Repeater ID="repResults" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>Stop ID</td>
<td>Stop Name</td>
<td>On Street</td>
<td>At Street</td>
<td>Latitude</td>
<td>Longitude</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem, "StopID")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "StopName")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "OnStreet")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "AtStreet")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "StopLat")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "StopLon")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>