简体   繁体   English

为什么我的jQuery AJAX函数总是返回false?

[英]Why does my jQuery AJAX function always return false?

function check_username(){
    $.ajax({
        type: "POST",
        dataType: 'json',
        url: "/ajax/check/username.html",
        data: "via=ajax&username="+$('input[name=register_username]').val(),
        success: function(msg){
            if(msg.response==false){
                register_username.parent().css('background-color','#db2e24');
                register_username.parent().parent().find('td:last-child').text(msg.message);
                register_username.focus();
                return false;
            } else {
                register_username.parent().css('background-color','#fff');
                register_username.parent().parent().find('td:last-child').text("");
                return true;
            }
       }
    });
}

I'm sorry if my English isn't good -- English is not my native language. 如果我的英语不好,我很抱歉-英语不是我的母语。 Back to the topic, why does the function above always return false? 回到主题,为什么上面的函数总是返回false? FYI : the JSON is OK 仅供参考:JSON可以

check_username calls an ajax function which starts a networking operation and then returns immediately. check_username调用ajax函数,该函数启动网络操作,然后立即返回。 check_username returns long before the ajax call finishes and the success handler gets called. check_username在ajax调用完成和成功处理程序被调用之前很久就返回。 Thus, the success handler has NOTHING to do with the value that check_username returns. 因此,成功处理程序与check_username返回的值check_username

Since there is no return value in the check_username function itself (only in the embedded success handler function), check_username returns undefined which is a falsey value, thus you think it's always returning false. 由于check_username函数本身没有返回值(仅在嵌入式成功处理程序函数中),因此check_username返回undefined ,它是一个false值,因此您认为它总是返回false。

If you want to do something with the return value from the success handler, then you have to either operate in the success handler itself or you have to call another function from the success handler. 如果要对成功处理程序的返回值进行处理,则必须在成功处理程序本身中进行操作,或者必须从成功处理程序中调用另一个函数。 This is how asynchronous operations work. 这就是异步操作的工作方式。

Returning true or false from the success handler function does nothing. 从成功处理程序函数返回truefalse不会执行任何操作。 The success handler is called by the internals of the ajax handling code and returning from the success handler just goes into the bowels of the ajax internals. 成功处理程序由ajax处理代码的内部调用,从成功处理程序返回的内容仅进入ajax内部组件的肠子。 The return value from the success handler is not used in any way. 成功处理程序的返回值不以任何方式使用。

The problem is the logical condition msg.response==false.. this always evaluates to false, the response is not a boolean. 问题是逻辑条件msg.response == false ..总是评估为false,响应不是布尔值。 You need to check the response status instead. 您需要检查响应状态。

If I had to guess, you post, and the response returned is a JSON object. 如果我不得不猜测,可以发布,返回的响应是一个JSON对象。 But it looks like your just checking to see if you got a valid response. 但看起来您只是在检查是否收到有效回复。 Why not try it this way: 为什么不这样尝试:

Try this: 尝试这个:

function check_username()
        {
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: "/ajax/check/username.html",
                data: "via=ajax&username="+$('input[name=register_username]').val(),
                success: function( msg, textStatus )
                {
                    if ( textStatus == "success" )
                    {
                        register_username.parent().css('background-color','#db2e24');
                        register_username.parent().parent().find('td:last-child').text(msg.message);
                        register_username.focus();
                        return false;
                    } else {
                        register_username.parent().css('background-color','#fff');
                        register_username.parent().parent().find('td:last-child').text("");
                        return true;
                    }
                },
                error: function( jqXHR, textStatus, errorThrown )
                {
                    if ( textStatus == "parsererror" )
                    {
                        alert( "There is an error in your JSON object" );
                    }
                }
            });
        }

The other problem you could have is that your not returning valid json, which jQuery will check. 您可能遇到的另一个问题是您未返回有效的json,而jQuery会对其进行检查。 Adding an error will help reveal if this is indeed the case. 添加错误将有助于揭示是否确实如此。

Live demo http://jsfiddle.net/khalifah/4q3EJ/ 现场演示http://jsfiddle.net/khalifah/4q3EJ/

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

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