douni1396 2014-03-24 19:51
浏览 12

没有调用jQuery [number]

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!

  • 写回答

1条回答 默认 最新

  • dougong5817 2014-03-24 19:54
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100