I am using ajax to make a website that can monitor the SEPTA bus schedule. However my web always got error when sending request to the URL(http://www3.septa.org/hackathon/TransitView/?route=ROUTE_NUAMBER&callback=?
).
This is the function that I used before:
function getSchedule(){
var route = $("#route");
var myURL = "http://www3.septa.org/hackathon/TransitView/?route=" + route.val() + "&callback=?";
$.ajax({
type: "GET",
url: myURL,
contentType: "application/json; charset-utf-8",
data: "()",
datatype: "jsonp",
success: function(json){
var myTable = "<tr><td>Vehicle ID</td> <td>Block ID</td> <td>Direction</td> <td>Destination</td> <td>Delay</td></tr>";
for(var i=0; i<json.bus.lenght; i++){
var vehicleNumber = json.bus[i].VehicleID;
var blockID = json.bus[i].BlockID;
var direction = json.bus[i].Direction;
var destination = json.bus[i].destination;
var offset_sec = json.bus[i].Offset_sec;
var offset_sec_n = parseInt(offset_sec);
if(offset_sec_n >= 120){
var delay = "Delay";
}
else{
var delay = "On Time";
}
myTalbe += "<tr><td>" + vehicleNumber + "</td><td>" + blockID + "</td><td>" + direction + "</td><td>" + destination + "</td><td>" + delay + "</td></tr>";
}
myTalbe = "<tb>" + myTable + "</tb>";
document.getElementById("schedule").innerHTML = myTable;
},
error: function(xhr, ajaxOptions, thrownError){
document.getElementById("schedule").innerHTML = "Error fetching "
+ myURL;
}
})
}
It got this erorr message:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
Then, I got a modification suggestion:
url: "http://ww3.septa.org/Hackathon/TransitView/",
data: {route: route_val}, //where route_val is whatever variable stores the route number. it can be named route, I just wanted to clarify the key vs value
jsonpCallback: "callback_func" //note that this is a string.
But I am not really understand what the CallBack_function
is. How can I make my web works?