无法在 ci 中传递数据 ajax

Cannot pass data ajax in ci

$("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 我传递给响应控制器

您的 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);
    }
  });
});

换个ajaxcode.Controller就可以了

$("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);
    }
  });
});