Js function returning undefined though return value is bool

时间:2018-10-02 09:17:42

标签: javascript ajax return

I've got a weird behaviour in js. I use the following function to check if a username or email is already used on input change (in a registration form):

function ajaxCheck(type)
{
    var data;

    if (type === 'username')
        data = $('#form_username').val();

    else if (type === 'email')
        data = $('#form_email').val();

    else return;

    $.ajax({
        method: "POST",
        url: "register_check/" + type,
        data: {data: data}
    }).done(function (ret) {
        ret = (ret === "true"); // to get boolean instead of string
        alert(ret);
        return ret;
    })
}


$('#TEST_BTN').click(function () {
       alert(ajaxCheck('username'));
    });

I use a test button for debug purposes. The problem is: when clicking on the button, alert(ret) returns true or false as wanted (the php is tested and works) but alert(ajaxCheck('username')) returns undefined... why ? (I'm using symfony 4.1.5 if that changes anything although I think not)

0 个答案:

没有答案