ajax调用的结果转到错误函数

时间:2015-12-17 06:49:20

标签: php ajax

嘿iam试图建立一个安卓包,后端在带有phonegap的php上。所有人都完美地在localhost上工作。在服务器中我无法登录。使用ajax检查登录,但它没有成功。但我可以在localhost上工作。问题是什么?你能帮助我吗?

function handleLogin() { 
    var fnUrl = base_url+"auth/mob_log";   
    var form = $("#loginForm");  
    var u = $("#username", form).val();
    var p = $("#password", form).val(); 
    if(u != '' && p!= '') { 
        $.ajax({
            url: fnUrl,
            data: {"u": u,"p":p}, //returns all cells' data
            dataType: 'json',
            type: 'POST',
            success: function (res) {
                if (res.log == '1') {
                    window.localStorage["username"] = u;
                    window.localStorage["password"] = p;              
                    checkLoginAuth();           

                } else{ 
                    alert(res.msg)  
                    $("#edLogin").removeAttr("disabled");                
                }
            },
      //this errormsg is showing while login

            error: function () {
                alert('Something went wrong.Contact Support')
            }
        });
    } 
    else {
        alert("Enter Username and Password") 
        $("#edLogin").removeAttr("disabled");
    }
    return false;
}

PHP

function mob_log() {     

    $username = $this->input->post('u');      
    $password = $this->input->post('p');      
    $login_result = $this->ion_auth->login_mob($username,$password);   
    if($login_result == 1)
    {
        $this->load->model('login_model');                       
        $p_details=$this->login_model->get_p_details($this->input->post('u'));  
        $p_id = $p_details->id;
        $out = array(
        'log' => '1',
        'p_id' => $p_id,
        'msg'=>'Logged in'
        );   
        echo json_encode($out); 
    }
    else
    {    
        $out = array(
        'log' => '0',
        'msg'=>'Unable to login'
        );   
        echo json_encode($out);             
    }

}

1 个答案:

答案 0 :(得分:0)

试试这个:

var data = "u": u,"p":p};
$.post(fnUrl, data)
                .done(function (res) {
                    alert(res);
                    res = $.parseJSON(res);
                    if (res.log == '1') {
                         window.localStorage["username"] = u;
                         window.localStorage["password"] = p;              
                         checkLoginAuth();           

                    } else{ 
                         alert(res.msg)  
                          $("#edLogin").removeAttr("disabled");                
                    }
                });

此外,使用console.log和alert函数来跟踪值的内容。