在CI控制器中无法识别从json传递的数据值

时间:2013-10-24 11:36:51

标签: php codeigniter

这个代码应该工作正常,据我所知,但由于某种原因它不起作用。有人能帮帮我吗?!! T_T

js script:

$.ajax({

            type: 'POST',
            url: '<?=base_url()?>/folder_name_ajax/unit_vacancy',
            dataType: 'json',
            data:{unit_id:unit_id, unit_landlord_id:unit_landlord_id, unit_stat:unit_stat},

            beforeSend: function(){ 

            },
            success: function(response){ 
                console.log(response);
                alert('Unit status successfully updated.');
            }

        });

控制器:

function unit_vacancy()
{
    $this->load->model('modelAjax');

    $unit_id            = mysql_real_escape_string($this->input->post('unit_id'));      
    $unit_landlord_id   = mysql_real_escape_string($this->input->post('unit_landlord_id'));
    $unit_stat          = mysql_real_escape_string($this->input->post('unit_stat'));
    $xplod_stat         = explode('-',$unit_stat);

    $this->modelAjax->update_unit_vacancy();

}

型号:

function update_unit_vacancy()
{
    $unit_id            = mysql_real_escape_string($this->input->post('unit_id'));      
    $unit_landlord_id   = mysql_real_escape_string($this->input->post('unit_landlord_id'));
    $unit_stat          = mysql_real_escape_string($this->input->post('unit_stat'));
    $xplod_stat         = explode('-',$unit_stat);

    $result = $this->db->query("SELECT COUNT(ID) AS count FROM ".TBL_PREFIX."".UNIT_VACANT_TBL." WHERE unit_id = '$unit_id' ");
    $count = $result->num_rows();

    if($count == 0)
    {
        $result = $this->db->query("INSERT INTO ".TBL_PREFIX."".UNIT_VACANT_TBL."
                                    (
                                        unit_id,
                                        landlord_id,
                                        status
                                    )
                                    VALUES
                                    (
                                        '$unit_id',
                                        '$unit_landlord_id',
                                        'insert'
                                    )
                                     ");

    }
    else
    {
        $result = $this->db->query("UPDATE ".TBL_PREFIX."".UNIT_VACANT_TBL." 
                                    SET 
                                        status = 'update'
                                    WHERE 
                                        unit_id = '$unit_id' 
                                  ");
    }
}

我似乎无法理解我在这里做错了什么!!!

1 个答案:

答案 0 :(得分:1)

我看到你的控制器中没有echo(ing)任何东西。你的json在哪里?您的update_unit_vacancy()返回json吗?如果yes然后echo它。

 echo $this->modelAjax->update_unit_vacancy();

并检查您的控制台是否有ajax的响应。不过会再向你推荐一件事,改变下面一行:

url: '<?=base_url()?>index.php/rentdaddy_ajax/unit_vacancy',