I am trying to obtain data in jsonp format so that I can work with phonegap. But on the web browser it doesn't work and there are no error showing in the console. Clearly, something must be wrong with the code, But as I have never done jsonp before If somebody could help please. php api
function airportlist_get() {
$this->load->database();
$sql = 'SELECT * FROM airport order by airport;';
$sql2 = 'SELECT COUNT(a_id) AS records FROM airport';
$query2 = $this->db->query($sql2);
$data2 = $query2->row();
$info->records = intval($data2->records);
$query = $this->db->query($sql);
$data = $query->result();
$info->airports = $data;
$callback = isset($_GET['callback']) ? preg_replace('/[^a-z0-9$_]/si', '', $_GET['callback']) : false;
header('Content-Type: ' . ($callback ? 'application/javascript' : 'application/json') . ';charset=UTF-8');
$json = json_encode($info);
$jsonp_callback = isset($callback) ? $json: null;
$this->response($jsonp_callback, 200);
}
Ajax call:
function Getairports(){
$.ajax({
url: 'http://creative.coventry.ac.uk/~airports/airports/v1.0/index.php/airport/airportlist',
dataType: 'jsonp',
success: function(data) {
$("#airportlist").empty();
$.each(data.airports,function(i,airport){
$('#airportlist').append('<li> <a href="#shopbyairport" data-transition="slide" onclick="Getshops('+airport.a_id+')">'+airport.airport+'</a></li>');
a_id = airport.a_id;
console.log(a_id);
});
$.mobile.changePage("#airports");
$('#airportlist').listview('refresh');
},
error: function (response) {
var r = jQuery.parseJSONP(response.responseText);
alert("Message: " +r.error.text);
}
}
)};
If you check the error log in the browser, you will see that you get a syntax error when you request the file.
The response is not valid JSONP. You have this added at the end of the response:
"{\"records\":2,\"airports\":[{\"a_id\":\"2\",\"airport\":\"Birmingham International\"},{\"a_id\":\"1\",\"airport\":\"Luton Airport\"}]}"