JSONP parsererror, worked local
I have some jQuery which makes an ajax call to an url which returns json.
This works locally, but does not remote. This is the PHP code on the
remote side. I have insufficient privileges to change the remote PHP code.
So I would be really happy if someone has an idea to change the jQuery to
get it to work. If there are changes needed to the PHP, I can ask them.
But I have no guarantee that they will fix it.
function getdata($query){
$data_sql = mysql_query($query) or die(mysql_error() . '<br />'. $query);
$datas = array();
while($data = mysql_fetch_array($data_sql)){
$datas[] = $data;
}
$datas = json_encode($datas);
print( $_GET['callback'].'('.$datas.')');
}
As I said, this works local, but remote, it does not. I get an error on
the while loop. This is the output:
<br/>Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result resource in /public_html/api/data.php on line 19
([])
it is a cross domain request and there is no Cors header like
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
Can that be the cause? or is cors for later concern?
ps. lord_linus wanted to see my jQuery code:
$.ajax({
url: sUrl,
crossDomain: true,
dataType: "jsonp",
jsonpCallback: function (data) {
alert('call back works!'); // this one shows!
var viewModel = ko.mapping.fromJS({ items: data }, mapping, {});
ko.applyBindings(viewModel);
},
type: "GET",
contentType: "application/json;charset=utf-8",
headers: { "accept": "application/json;charset=utf-8" },
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr); alert(xhr.status); alert(xhr.responseText);
alert(ajaxOptions); alert(thrownError);
}
});
the jquery gives a parsererror. Chrome gives more details. I get the first
alert that the callback works and then it somehow changes the callback:
end of url is &callback=undefined&_=1376052141210 somehow? But I defined
jsonCallback??
No comments:
Post a Comment