Php jQuery没有向codeigniter发送数据

Php jQuery没有向codeigniter发送数据,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,我试图让ajax与codeigniter安装一起工作 这是我的PHP函数: function test() { return print_r("hey"); } 这是JS: $.ajax({ type: "POST", url: "http://localhost/code/test", success: function(data) {

我试图让ajax与codeigniter安装一起工作

这是我的PHP函数:

    function test() {
            return print_r("hey");
    }
这是JS:

    $.ajax({
          type: "POST",
            url: "http://localhost/code/test",
            success: function(data) {
                    alert(data);    
            }
    });
这工作得很好,但一旦我添加数据,它就不起作用了

    $.ajax({
          type: "POST",
            url: "http://localhost/code/test",
            data: {bar:"foo"}, 
            success: function(data) {
                    alert(data);    
            }
    });

提前谢谢

请检查以下内容

$.ajax({
          type: "POST",
            url: "http://localhost/code/test",
            data: "&bar=foo&isAjax="+true, 
            success: function(data) {
                    alert(data);    
            }
    });
和控制器

function test() {
            if($this->input->post('isAjax')){
                  return print_r("hey");
            }
            else{
                  //do another thing
            }

    }
还有一件事,如果要添加json格式的数据,则必须在$.ajax对象中添加另一个属性,即数据类型:json使用以下内容:

$.ajax({
          type: "POST",
          url: "http://localhost/code/test",
          dataType: 'json',
          data: {'bar':'foo'},
          success: function(data) {
                  alert(data);    
          }
    });
$.ajax({
    type: "POST",
    url: "http://localhost/code/test",
    data: "bar=foo&name=cyberbob", 
    success: function(data) {
            alert(data);    
    }
});
或者您可以使用速记版本:

$.post("http://localhost/code/test", {'bar':'foo'}, function(data) {
    alert(data);
});
您的php代码应该是:

function test() {
    echo "hey";
}
请尝试以下操作:

$.ajax({
          type: "POST",
          url: "http://localhost/code/test",
          dataType: 'json',
          data: {'bar':'foo'},
          success: function(data) {
                  alert(data);    
          }
    });
$.ajax({
    type: "POST",
    url: "http://localhost/code/test",
    data: "bar=foo&name=cyberbob", 
    success: function(data) {
            alert(data);    
    }
});

这两个代码段上的url不同,这是故意的吗?哪一个是发送请求的正确url?http://localhost/test 或http://localhost/code/test 正确的url是我不知道它们为什么不同。