Php 如何从CI_控制器向AJAX发送/返回数据/值

Php 如何从CI_控制器向AJAX发送/返回数据/值,php,ajax,codeigniter,web,Php,Ajax,Codeigniter,Web,作为标题,我想让CI_控制器将值/数据返回给之前发出请求的AJAX _Controller.php class The_Controller extends CI_Controller { public function __construct() { parent::__construct(); } function postSomething() { $isHungry = true; if(isHu

作为标题,我想让CI_控制器将值/数据返回给之前发出请求的AJAX

_Controller.php
class The_Controller extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    function postSomething()
    {
        $isHungry = true;
        if(isHunry){
            return true;
        }else{
            return false;
        }
    }
}

AJAX

class The_Controller extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    function postSomething()
    {
        $isHungry = true;
        if(isHunry){
            return true;
        }else{
            return false;
        }
    }
$(文档).ready(函数() {

        $('#form').on('submit', function (e)
        {
            e.preventDefault(); // prevent page reload
            $.ajax ({
                type : 'POST', // hide URL
                url : '/index.php/The_Controller/postSomething',
                data : $('#form').serialize (),
                success : function (data)
                {
                    if(data == true)
                    {
                        alert ('He is hungry');
                    }
                    else
                    {
                        alert ('He is not hungry');
                    }       
                }
                , error: function(xhr, status, error)
                {
                    alert(status+" "+error);
                }
            });
            e.preventDefault();
            return true;
        });
    });
问题:

class The_Controller extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    function postSomething()
    {
        $isHungry = true;
        if(isHunry){
            return true;
        }else{
            return false;
        }
    }
上面的AJAX代码对于变量data总是获取FALSE。它不是从CI_控制器中的postSomething函数获取返回值

class The_Controller extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    function postSomething()
    {
        $isHungry = true;
        if(isHunry){
            return true;
        }else{
            return false;
        }
    }
问题:

class The_Controller extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    function postSomething()
    {
        $isHungry = true;
        if(isHunry){
            return true;
        }else{
            return false;
        }
    }
我想知道(data==true)结果警报(‘他饿了’);但会导致数据不被CI_控制器内postSomething函数的返回值填充,因此它总是false。如何从CI_控制器获取返回值/数据到AJAX?

class The_Controller extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    function postSomething()
    {
        $isHungry = true;
        if(isHunry){
            return true;
        }else{
            return false;
        }
    }

谢谢

您需要稍微更改代码。无需返回数据,只需从控制器返回
echo true
echo false
。希望您能得到所需的结果。

您的PHP代码中有一个输入错误

    if(isHunry){
应该是

    if($isHungry){
另外,将数据返回到AJAX请求时,确实应该在header.Ex中发送正确的内容类型

header('Content-Type: application/json');
打印
回显
数据,而不是
返回
数据,以及
json\u编码
数据:

echo json_encode($data);
因此,您的
postSomething
php函数应该如下所示:

$isHungry = true;
header('Content-Type: application/json');
echo json_encode($isHungry);

您必须像echo$isHungry;或echo json_encode($isHungry)那样打印;还可以在ajax调用中为json数据添加dataType:json。