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.

查看全部
weixin_33712987
weixin_33712987
2013/05/24 12:13
  • ajax
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信

1个回复