Reputation: 1950
Ok, I have searched this question and have found numerous answers on it but none have worked for me. I'm getting jQuery21006460414978209883_1395689439888 was not called error. The AJAX call is done right, status is 200 and the php url that i'm trying to call, i have validated its JSON and it is a valid json according to jsonlint. I'm using jsonp since it is a crossdomain call. Any advise would help me debug this. Here is my javascript code:
$.ajax({
crossDomain: true,
cache: false,
type: "GET",
url: "http://example.com/backend.php",
data: "fdaf",
//async: false,
dataType: "jsonp",
contentType: 'application/json; charset=utf-8',
success: function(msg){
alert("success"+ JSON.stringify(msg));
},
error:function (xhr, ajaxOptions, thrownError){
alert('error function status is : '+xhr.status);
alert('error thrown is: '+thrownError);
},
jsonp: "callback",
complete: function (requestState) {
alert("working");
}
});
My backend.php code is here:
<?php
header('Content-Type: application/json');
$arr = array('a' => "1", 'b' => "2", 'c' => "3", 'd' => "4");
$tr = json_encode($arr);
echo $_GET['callback'].'['.$tr.']';
?>
Please advise what I am doing wrong here. Many thanks!
Upvotes: 1
Views: 914
Reputation: 49919
Your issue is this, the output of your PHP:
callbackfunction[[DATA]]
This does not call a Javascript function. You should have this output:
callbackfunction([DATA])
With this PHP:
echo $_GET['callback'].'('.$tr.')';
Because $tr
is an Array it will return [DATA]
You should only need JSONP
if you do a Cross-Domain request. Otherwise just use a GET / POST.
Upvotes: 2