Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个Ajax代码点火器调用不起作用?_Ajax_Codeigniter - Fatal编程技术网

为什么这个Ajax代码点火器调用不起作用?

为什么这个Ajax代码点火器调用不起作用?,ajax,codeigniter,Ajax,Codeigniter,我有点困惑,为什么这个CI-Ajax调用不起作用?如果我将$SearchTerm设置为字符串,我会收到一个响应,但它不会从AJAX收到“搜索”帖子?代码点火器中是否有需要更改的设置?谢谢 jQuery.ajax({ type: "GET", url: 'home/getAjaxData/', data: 'search=1', dataType: "json", success: function(resp) { Al

我有点困惑,为什么这个CI-Ajax调用不起作用?如果我将$SearchTerm设置为字符串,我会收到一个响应,但它不会从AJAX收到“搜索”帖子?代码点火器中是否有需要更改的设置?谢谢

jQuery.ajax({
      type: "GET",
      url: 'home/getAjaxData/',
      data: 'search=1',
      dataType: "json",
      success: function(resp) {
         Alert (resp);
      }
  });

 public function getAjaxData() {
    $SearchTerm = $this->input->post('search');
    echo $SearchTerm;       
 }
$(函数(){
$.ajax({
类型:“POST”,
url:“”,
数据:{搜索:'1'},
数据类型:“json”,
成功:功能(resp){
警报(resp);
}
});
});

您的代码$this->input->post('search'),我认为它只适用于CodeIgniter中的post。您可以使用jQuery获取textbox的值,例如:

<input type="textbox" name="text" /><br/>
$('input[name="text"]').val();

如果ajax GET调用在CI中工作正常,但ajax POST不工作,请检查CSRF保护。为测试禁用它(application/config/config.php):


如果ajax POST调用开始工作,您可以再次启用保护,并将ci_csrf_令牌添加为ajax数据的一部分。

将其更改为
POST
,而不是
GET

 jQuery.ajax({
      type: "POST",
      url: 'home/getAjaxData/',
      data: 'search=1',
      dataType: "json",
      success: function(resp) {
         alert(resp);
      }
  });
由于您是通过POST发送数据,因此此is功能将正常工作

public function getAjaxData() {
    $SearchTerm = $this->input->post('search');
    echo $SearchTerm;       
 }

GET
更改为
POST
$config['csrf_protection']  = FALSE;
 jQuery.ajax({
      type: "POST",
      url: 'home/getAjaxData/',
      data: 'search=1',
      dataType: "json",
      success: function(resp) {
         alert(resp);
      }
  });
public function getAjaxData() {
    $SearchTerm = $this->input->post('search');
    echo $SearchTerm;       
 }