weixin_33695082 2015-09-22 07:14 采纳率: 0%
浏览 14

Ajax通话没有警报

I´m trying to alert some data from a Ajax call. Can anyone spot what I am doing wrong?

PHP

while($row = $stmt->fetch()){
    echo json_encode($row);
}
echo "Done!";

Result from Json_encode / Network Preview

{"flakId":"21098-10_flak-2"}{"flakId":"21098-10_flak-1"}Done!

JS

if(chosenObjNr){
    alert('I CAN see this alert')
    $.ajax({
        url:'php/update.php',
        type: 'POST',
        data: 'chosenObjNr=' + chosenObjNr,
        dataType: 'json',
        error: function (data){ alert("failed, i can see this!");},             
        success: function(data){  

            alert('I cannot see this alert!');
            alert(data);

            var data0;
            data0 = data[0];
            alert(data0);

            var falkId;
            flakId = data[0];
            alert(flakId);

            console.log(data);
        }

    });

};

RESULT

No alert and nothing in the console.

  • 写回答

1条回答 默认 最新

  • weixin_33747129 2015-09-22 07:58
    关注

    The problem is, that the success part of the ajax never will run. You defined the dataType as a json, so it expecting json. Your PHP is echos 2 json and an unwanted string.

    So, to check what is your error add the fail function as Sina sad in the comment:

    if (chosenObjNr) {
    alert('I CAN see this alert')
    $.ajax({
        url: 'php/update.php',
        type: 'POST',
        data: 'chosenObjNr=' + chosenObjNr,
        dataType: 'json',
    }).done(function (data) {
        alert('I cannot see this alert!');
        //Do what you want to do here
    }).fail(function (msg) {
        alert('An error occured: ' + msg.statusText);
    });
    

    }

    And, if you want to fix your .php remove the echo 'Done'; part, and add your records to an array, and when its done, encode it to json:

    $return = array();
    while ($row = $stmt->fetch()) {
        $return[] = $row;
    }
    echo json_encode($return);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 u盘问题:盘符不显示 无媒体
  • ¥50 R语言读取nc按月均值转为tif
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题