Codeigniter Rest和Ajax没有得到理想的结果

时间:2014-02-04 13:29:13

标签: php ajax codeigniter rest

我的codeigniter控制器设置如下:

<?php

include('./application/libraries/REST_Controller.php');

class Restful extends REST_Controller{

public function user_get()
{
    //URL as such:
    //  http://localhost/rest/restful/user/id/1
    //notice array('returned'$this->get(id)) ->>>> get the id thats being sent 
    $data = array('returned: '.$this->get('id'));
    $this->response($data);
    //returns :
    //  xml <item> returned: 1</item> /xml
    }
}

尝试从设置为这样的视图中访问它:

<!DOCTYPE html>
<html>
<header>
<title style="font-family:Logo">Ajax</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--this is jQuery with only the ajax part-->
<script src="../ajax.js"></script>
</header>
<body>

<div id="e">

</div>


<script>
function getrest(){
    var output;
    res=$.ajax({
        url: 'http://localhost/rest/restful/user?id=1',
        type: 'GET',
        success: function(output){
            document.getElementById('e').innerHTML = output;
        }
    });
}
$(document).ready(function(){
    getrest();
});
</script>
</body>
</html>

我的问题是我从视图中得到的结果是: [对象文件]

我在这里做错了什么?

编辑: 如果我在URL中键入此内容 休息/宁静/用户?ID = 1

我从其他控制器得到这个结果:          返回:1     

由于 杰森

1 个答案:

答案 0 :(得分:-1)

我认为您需要将函数名称定义为“user”,而不是“user_get”。所以控制器代码将是

public function user()
{

$data = array('returned: '.$this->get('id'));
$this->response($data);

}
}