无法在ci中传递数据ajax

时间:2015-02-25 05:45:30

标签: ajax codeigniter codeigniter-2 codeigniter-form-helper

$("body").on('click', '.name', function(e) {
   //var valueofbutton = $(this).val();         
            $.ajax({
     type: "POST",
     url: "response",
     data: "name=John&location=Boston",
     success: function(msg){
        alert( "Data Saved: " + msg );
     }
});

});

我的回应控制器

Class Response extends CI_Controller{


public function index()
{

$data=$this->input->post('name'); 

echo $data;

}


}

它向我展示了一些错误,我不知道它是什么样的错误!

我的提醒会提供此信息

Data Saved: <br />
<b>Warning</b>:  Unterminated comment starting line 25 in <b>C:\xampp\htdocs\vacationgod\application\controllers\response.php</b> on line <b>25</b><br />
John

我无法看到任何打印信息,例如我传递给响应控制器的john

2 个答案:

答案 0 :(得分:1)

您的data应该是一个对象并删除.name。像你这样更改你ajax中的那一行:

$("body").on('click', function(e) {
  $.ajax({
    type: "POST",
    url: "response.php",
    data: {name: "John",location:"Boston"},
    success: function(msg) {
      alert("Data Saved: " + msg);
    }
  });
});

答案 1 :(得分:1)

只需更改你的ajax代码。控制器就可以了。

$("body").on('click', function(e) {
  $.ajax({
    type: "POST",
    url: '<?=base_url("controller_name/function_name") ?>',
    data: {name: "John",location:"Boston"},
    success: function(response) {
      alert("Data Saved: " + response);
    }
  });
});