weixin_33712987 2013-05-24 12:13 采纳率: 0%
浏览 88

jQuery Ajax方法未定义

So my problem is that data returned in success method is undefined...

Here is my jquery code:

  $('document').ready(function(){



      $('#save').click(function() {

      var dataString = $("#stepform").serialize();

      $.ajax({
     url: "<?php echo base_url();?>create/save/<?php echo $row->id; ?>",
     type: 'POST',
     data: dataString,
     success:function(response){

        alert(response.status);

     }
    });//end ajax

    return false;
});//end click

  }); // end document ready

And php code:

$serverResponse["status"] = 'it worked';
echo json_encode($serverResponse);

And all i'm getting from response.status is 'undefined'... I just can't make it work! Any ideas what i'm doing wrong?

-------------EDIT-------------

Finally i have managed to find a solution to invalid json response. If you are using codeigniter you must write exit() after json_encode. Something like this:

exit(json_encode($yourarray));

If you just echo it then it gives a parse error.

  • 写回答

1条回答 默认 最新

  • perhaps? 2013-05-24 12:15
    关注

    jQuery may not be properly guessing that you are returning JSON. Set the dataType: "json" property on the $.ajax argument object, or add

    header("Content-type: application/json");
    

    to your PHP script before emission, or both.

    评论

报告相同问题?

悬赏问题

  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数