PhoneGap jquery移动超时(永不点击)

时间:2014-07-18 11:22:47

标签: javascript jquery-mobile cordova

我正在通过phonegap-build向我的网络服务器发出请求,但我的超时时间从未被击中(如下所示,我已经尝试将时间设置为5000毫秒进行测试...当服务器没有时...回应,我无法解释为什么,任何想法?

$.ajax({
    crossDomain: true,
    timeout: 5000,
    contentType: "application/json; charset=utf-8",
    url: "http://somedomain.com/Register.asmx/Login",
    data: { UserName: user, Password: password },
    dataType: "jsonp",
    success: onDataReceived,
    error: noDataReceived
});

function onDataReceived(data)
{
    $.mobile.loading( 'hide', { theme: "b", text: "", textonly: false});

    if(data.SessionID.length > 7){
        localStorage.setItem("SessionID",data.SessionID);
        localStorage.setItem("Username", $('#homeUserName').val());
        console.log("Session Login Set");
        reReg();

        $.mobile.changePage("#mainpage");


    } else {
        console.log("Password error, not Set");

        function alertDismissed() {

        }
        navigator.notification.alert('Sorry, incorrect login credentials.', alertDismissed, 'Error', 'Okay' );

    }
}

function noDataReceived(x, t, m)
{
    $.mobile.loading( 'hide', { theme: "b", text: "", textonly: false});
        navigator.notification.alert('Sorry, no data returned. Are you connected to the internet?', alertDismissed, 'Error', 'Okay' );
}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法,具体取决于您使用的JQ版本。这是未经测试的,因为我没有json url可以使用。

jQuery 1.8 +

$.ajax({
    crossDomain: true,
    contentType: "application/json; charset=utf-8",
    url: "http://somedomain.com/Register.asmx/Login",
    data: { UserName: user, Password: password },
    dataType: "jsonp",
    timeout:5000 //5 second timeout
}).done(function(){
    //do something

   onDataReceived(data);


}).fail(function(jqXHR, textStatus){
    if(textStatus == 'timeout')
    {     
        alert('Failed from timeout'); 
        //do something. Try again perhaps?
    }
});​

jQuery< = 1.7.2

$.ajax({
    crossDomain: true,
    contentType: "application/json; charset=utf-8",
    url: "http://somedomain.com/Register.asmx/Login",
    data: { UserName: user, Password: password },
    dataType: "jsonp",
    error: function(jqXHR, textStatus){
        if(textStatus == 'timeout')
        {     
             alert('Failed from timeout');         
            //do something. Try again perhaps?
        }
    },
    success: function(){
        //do something

    onDataReceived(data);

    },
    timeout:5000 //5 second timeout
});