【问题标题】:Response is not getting in codeigniter ajax function响应没有进入 codeigniter ajax 函数
【发布时间】:2014-03-14 06:26:04
【问题描述】:

我的控制器功能是这样的

function check_user() {
        $user_name = $this->input->post('user_name');
        $res_user = $this->user_model->is_unique_user($user_name);
}

我有如下的用户 ajax 代码

success: function(response){
                                if(response.data == 'exist'){
                                    $("#user_name").after('<p class="help-block">Username alredy exist</p>');
                                }
                                                                    else{
                                                                            $("#user_name").after('<p class="help-block">Username available</p>');
                                } 

这是我的模型函数

function is_unique_user($user_name)
{
    $this->db->select('*');
    $this->db->where("user_name = '$user_name'");
    $result = $this->db->get('tb_user');

    if($result->num_rows())
    {
        return $date = 'exist';
    }
    else
    {
        return $data = 'available';
    }
}

现在的问题是查询工作正常。但没有错误信息。我使用火虫控制台进行了检查。它显示没有响应的错误。这里有什么问题?

【问题讨论】:

    标签: jquery ajax codeigniter response


    【解决方案1】:

    请使用die 停止代码并返回响应。

    function check_user() {
            $user_name = $this->input->post('user_name');
           $res_user = $this->user_model->is_unique_user($user_name);
           die($res_user);
    }
    

    并且ajax成功函数将response.data改为response

    success: function(response){
            if(response == 'exist'){
                 $("#user_name").after('<p class="help-block">Username alredy exist</p>');
             }else{
                 $("#user_name").after('<p class="help-block">Username available</p>');
           } 
    

    【讨论】:

    • 显示“可用”。你能检查那个ajax部分吗?我在那里有点困惑
    • @joelcj,我已经更新了答案,ajax成功函数的问题,请将response.data更改为response,因为html中的ajax响应。
    • 又是一个问题。我得到回应。但我需要在页面中显示这个。如果条件不起作用。在控制台中,我可以根据需要看到响应。但页面中没有错误消息
    【解决方案2】:

    请查看此代码,使用echo 获得回复

    function check_user() {
            $user_name = $this->input->post('user_name');
           echo   $res_user = $this->user_model->is_unique_user($user_name);
    }
    

    【讨论】:

    • 试过那个家伙。它打印“可用”
    • 我认为问题在于在 ajax 代码中进行响应。请检查那里
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多