简体   繁体   English

jQuery不返回true或false

[英]jQuery not returning true or false

so I do this: 所以我这样做:

function loginFunc(userNameOrEmail, passWord){
        var emailOrUserNameV = encodeURIComponent(userNameOrEmail);
        var passWordV = encodeURIComponent(passWord);
        $.getJSON("http://mysite.com/pullData/login.php?callback=?",
              {
                emailOrUserName: emailOrUserNameV,
                passWord: passWordV
              },
              function(recievedData) {
                if(recievedData[0] == 'true'){
                    return true;
                }
                else{
                    return false;
                }

        });

}

then I do this: 然后我这样做:

$('#loginBtnHtml').click(function(){
            var emailVar = $('#email_html').val();
            var pwdVar = $('#password_html').val();
            if(loginFunc(emailVar, pwdVar)){
                alert('good');
            }
            else{
                alert('bad');
            }

        });

If I run the loginFunc() with an alert(recievedData[0]); 如果我运行带有alert(recievedData[0]); loginFunc() alert(recievedData[0]);loginFunc() alert(recievedData[0]); It works fine how do I get it to return bool to the $('#loginBtnHtml').click(); 我如何使布尔返回$('#loginBtnHtml').click(); ?

When I do this to troubleshoot: 当我这样做来排除故障时:

$('#loginBtnHtml').click(function(){
            var emailVar = $('#email_html').val();
            var pwdVar = $('#password_html').val();
            var whatIsBeingReturned = loginFunc(emailVar, pwdVar);
            alert(whatIsBeingReturned);

        });

it alerts with 'Undefined'... not sure what I'm doing wrong. 它会发出“未定义”警报...不确定我在做什么错。

You can't return from ajax. 您无法从ajax返回。 Unless the ajax is synchronous (don't do it!) You cannot depend on the code executing in any particular order. 除非ajax是同步的(否则不要这样做!)您不能依赖以任何特定顺序执行的代码。 All of the functionality that requires a response from ajax has to be in the ajax callback. 需要ajax响应的所有功能都必须在ajax回调中。

function loginFunc(userNameOrEmail, passWord){
    ...
    return $.getJSON("http://mysite.com/pullData/login.php?callback=?",
}

$("#loginBtnHtml").on('click', function () {
   //snip
   loginFunc(emailVar, pwdVar).done(function (whatIsBeingReturned) {
      console.log(whatIsBeingReturned);
   });
});

The response of the JSON request is asynchronous. JSON请求的响应是异步的。 The logicFunc() returns before the the response callback of the JSON request. logicFunc()在JSON请求的响应回调之前返回。

You need to write a callback method to handle the response. 您需要编写一个回调方法来处理响应。 Something like: 就像是:

function handleResponse(data){ 

     // do something
}


function loginFunc(userNameOrEmail, passWord){
        var emailOrUserNameV = encodeURIComponent(userNameOrEmail);
        var passWordV = encodeURIComponent(passWord);
        $.getJSON("http://mysite.com/pullData/login.php?callback=?",
              {
                emailOrUserName: emailOrUserNameV,
                passWord: passWordV
              },
              handleResponse

        });

}

Try a callback function instead. 请尝试使用回调函数。 The AJAX happens ansync and the completion function can't return a value to the wrapping function. AJAX发生异步,并且完成函数无法将值返回给包装函数。

function loginFunc(userNameOrEmail, passWord, callbackFn){
        var emailOrUserNameV = encodeURIComponent(userNameOrEmail);
        var passWordV = encodeURIComponent(passWord);
        $.getJSON("http://mysite.com/pullData/login.php?callback=?",{
            emailOrUserName: emailOrUserNameV,
            passWord: passWordV
        }, callbackFn);
}

$('#loginBtnHtml').click(function(){
    var emailVar = $('#email_html').val();
    var pwdVar = $('#password_html').val();

    loginFunc(emailVar, pwdVar, function(recievedData){
        alert(recievedData[0]);
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM