douyu4535 2014-05-18 15:29
浏览 33
已采纳

调试AJAX请求

I am currently trying to figure out how I can find out what errors are in my php script.

I have made a small script which an error on purpose. Using the chrome console I get...

Uncaught SyntaxError: Unexpected token < 

However, normally PHP errors would point you to the line in which the error occurs.

Here is my AJAX Request.

var myData = "Hello";

        $.ajax({ 
            type: "GET",
            url: 'test.php',
            datatype: 'json',
            data: { data: myData},
            success: function(output) {
                var result = $.parseJSON(output);
                for(var i=0; i<result.length; i++){
                    console.log(result[i]);
                }
            }
        });

And my PHP script with the syntax error

$data = ($_GET['data']);
echoo boom;
echo json_encode(array($data, "test"));

As you can see, line 2 is the error but the error in my console gives me no information as such.

Thanks

  • 写回答

4条回答 默认 最新

  • dorpbn1027 2014-05-18 15:36
    关注
    $.ajax({ 
        type: "GET",
        url: 'test.php',
        datatype: 'json',
        data: {},
        success: function(output) {
            result = output;
            for(var i=0; i < result.length; i++){
                console.log(result[i]);
            }
        }
    });
    

    By defining the dataType as json the output is already parsed. Parsing the already parsed 'output' again led to the error. So it was a client side / javascript error.

    ALso note that - in case an error occurs - the actual error message is parsed too. So you get an array containing the "fragmented" error message from the server ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

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