简体   繁体   中英

Unable to return a value from jQuery.ajax success function

I want to use jQuery Validator to check against the server when a user is signing up for if their desired username is already taken. The PHP script untaken.php does this job, returning ok if the username is available, or taken if it is taken.

My entire source is below, however the line in question is this:

return data != "taken";

Currently the message "Username already taken" permanently appears. However if I change it to:

console.log( data != "taken" );

and type in the text box I see in the console true and false messages exactly when I expect them. That's how I know the design itself isn't to blame, it's just the fact that I can't return anything from the success clause in jQuery.ajax.

$.validator.addMethod("userCannotBeTaken", function(value, element){

        var check;

        check = $.ajax({
            type: "GET",
            url: "untaken.php",
            data: ({ "user" : value }),
            dataType: "text",

            success: function(data){
                return data != "taken"; //return false if taken or true if not, based on response
            }
        });


    }, "Username already taken");

How can I return something from within jQuery.ajax ?

Turns out I was overthinking the whole thing and it can be easily done with jQuery Validator's built-in remote method. Here's my code if it helps someone:

Rule in validator script:

remote: "untaken.php"

PHP:

<?php

//mysql info
[snip]

//connect to database
$sql = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);

//Populate variable
$newun = $sql->real_escape_string( $_GET['newuser'] );


//Do the actual checks

$un_already_taken = $sql->query("
    SELECT username FROM logins WHERE username = '$newun'
"); //check if UN already in DB

if($un_already_taken->num_rows > 0) //un taken already
    print "false"; //tell jQuery to not submit form
else
    print "true"; //tell jQuery all is good

As easy as printing "true" or "false". Note it uses GET (ie. URL paramaters) not POST, and the GET variable is the name of the field it is validating (in my case newuser )

To return data to the ajax function, you need to print or echo the result in your untaken.php page.

soemething like:

echo $result;exit;

Then Try to alert the data inside success function and debug the result with

alert(data);return false;

The function you define for the success action is a callback.

When you make an ajax call, it is done asynchronously, (meaning the script continues executing while the browser connects to the server. You can't use return when dealing with callbacks because they are executed later, rather than immediately as a typical function.

This might make more sense: http://recurial.com/programming/understanding-callback-functions-in-javascript/

The success function should perform whatever actions that you want to happen after you get the data back to the server ie: alert the user that the username is taken.

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