简体   繁体   中英

Should I return true/false in JQuery Ajax function?

I was wondering if my code should have return statement in my AJAX function. Here is example:

$.ajax({
    type: 'POST',
    url: 'Application.cfc?method=runProcess',
    data: {'userID':userID},
    dataType: 'json'
}).done(function(obj){
    if(obj.STATUS == 200){
        $('#searchMsg').addClass("success").show().text(obj.MESSAGE).delay(3500).fadeOut('slow').queue(function(){
            $(this).removeClass("success").dequeue();
        });
        return true; //Should I keep this
    }else{
        $('#searchMsg').addClass("error").show().text(obj.MESSAGE).delay(3500).fadeOut('slow').queue(function(){
            $(this).removeClass("error").dequeue();
        });
        return false; //Should I keep this
    }
}).fail(function(jqXHR, textStatus, errorThrown){
    alert(errorThrown);
});

As you can see I have two return statements in my function. Both will return either true or false depends on the ajax return obj. Should I keep them in my function or not? What is benefit of having them and what are the cons? If anyone can explain please let me know. Thank you!

No need for this true or false if you send ajax call to check something and return server is exist they return true else false eg Login i send ma username and password to database if exist return true than i check if ajax return true mean they user login to the system if false means user not exist but not need for true false if exist you need to open new link else give error not exist its depend on what scenario you want but not necessary to return true false its not compulsory code example

$.ajax({
        url: geturl,
        type: "POST",
        data:{email:email,pass:pass},
        success: function (res) {
            if (res == "admintrue")
            {
                AutoLoader("Admin Login Succeffully", "success");
                var URL = $("#Afetlogin").val();
                window.location.href = URL;
            } else if (res == "membertrue") {
                AutoLoader("Member Login Succeffully", "success");
                var URL = $("#memberlogin").val();
                window.location.href = URL;
            }

            else {
                AutoLoader("Error", "error");
            }
        }
    })

No. Remove them. They do nothing for you. You already have the ability to know if the AJAX call succeeded or failed by which portion of your if/then in your callback function you hit.

All these statements do is send a value back to the caller that the caller then can use as it wishes and stop programmatic execution. Here, the caller is the done method call, which isn't expecting any return value and since you have the statements as the last things that will be done in the function, execution will stop anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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