Phonegap Rest API仅第二次工作

时间:2018-07-24 09:47:17

标签: javascript jquery phonegap codeigniter-restapi

我正在使用phonegap,但遇到了一个问题。我正在使用javascript ajax调用rest api。经邮递员检查后,API可以正常工作。但是,在Phonegap应用程序上,当我输入正确的用户名和密码时,该应用程序第一次不显示并刷新(闪烁),而在第二次当我输入相同的用户名和密码时,它运行正常。请帮助我是个书呆子。

$(document).ready(function()
{
$("#btn_signin").click(signinclickfunction);
}); //document ready


// $("#btn_signin").click(function()
function signinclickfunction()
{

    var appid="764df61a8dcd29d91207056e44f77a4714a13688";
    var username=$("#username").val();
    var password=$("#password").val();
    if(!username || !password)
    {   event.preventDefault(); 
        swal("Oops!", "Please Enter Valid Username And Password", "error");
        // sweetAlert('Congratulations!', 'Your message has been successfully sent', 'success');
    }
    else
    {
        var dataString="username="+username+"&password="+password+"&appid="+appid;
        $.ajax({
        type: "GET",
        url:"http://localhost:8080/monte_api_distributer/API_Login/checklogin",
        data: dataString,
        dataType:"JSON",
        crossDomain: true,
        cache: false,
        success: function(data){
            // var obj = JSON.parse(data);
            if(data.status==200)
            {   
                event.preventDefault();
                sweetAlert('Success!!', 'Logged In Successfully!!', 'success');
                $.each(data.result, function(i, field){
                    if(field.access_type==2)
                    {
                        //Teacher le login gareko
                        storeTeacherDataInStorage(field.id);

                    }
                    else if(field.access_type==3)
                    {
                        //Parents or Student le login gareko
                    }
                });
            }
            else if (data.status==404)
            {
                event.preventDefault();
                swal("Oops!", "Incorrect Username Or Password", "error");
                window.location.href="signin.html";
            }
            else if (data.status==401)
            {
                event.preventDefault(); 
                swal("Oops!", "Invalid Request. Please Try Again Later", "error");
                window.location.href="signin.html";
            }
            else
            {
                event.preventDefault(); 
                swal("Oops!", "Unknown Error Occured. Please Try Again Later", "error");
                window.location.href="signin.html";
            }
        },
        error:function (error){
            console.log(error);
            event.preventDefault(); 
            swal("Oops!", "There was an error processing your request. Please try again.", "error");
            window.location.href="signin.html";

        }
        });
    }


}//submit click

function storeTeacherDataInStorage(loginid)
{
    var appid="764df61a8dcd29d91207056e44f77a4714a13688";
    var dataString="appid="+appid+"&loginid="+loginid;
    $.ajax({
        type: "GET",
        url:"http://localhost:8080/monte_api_distributer/API_Login/getTeacherInfo",
        data: dataString,
        dataType:"JSON",
        crossDomain: true,
        cache: false,
        success: function(data){
            if(data.status==401)
            {
                event.preventDefault(); 
                swal("Oops!", "Invalid Request. Please Try Again Later", "error");
                window.location.href="signin.html";
            }
            else if(data.status==404)
            {
                event.preventDefault();
                swal("Oops!", "Incorrect Credentials", "error");
                window.location.href="signin.html";
            }
            else if(data.status==200)
            {
                $.each(data.result, function(i, field){
                    localStorage.setItem("fullname",field.full_name);
                    localStorage.setItem("userid",field.id);
                    // console.log(localStorage.getItem("userid"));
                    window.location.href="feedspage.html";
                });
            }
            else
            {
                event.preventDefault(); 
                swal("Oops!", "Unknown Error Occured. Please Try Again Later", "error");
                window.location.href="signin.html";
            }

        },
        error:function (error){
            console.log(error);
            event.preventDefault(); 
            swal("Oops!", "There was an error processing your request. Please 
try again.", "error");
            window.location.href="signin.html";
        }
        }); 
} //storing teacher data

The screenshot of postman showing api is working completely fine

0 个答案:

没有答案