submit form does not stop in jquery ajax call

前端 未结 6 1207
感动是毒
感动是毒 2020-11-29 12:29

I got following code :

$.ajax({
    type: \"POST\",
    async: false,              
    url: \"CheckIdExist\",
    data: param,
    success: function(result)         


        
6条回答
  •  一向
    一向 (楼主)
    2020-11-29 13:00

    You need to do a callback.

    This entry in the FAQ helped me a lot when I had this exact problem.

    getUrlStatus('getStatus.php', function(status) {
        alert(status);
    });
    
    function getUrlStatus(url, callback) {
        $.ajax({
            url: url,
            complete: function(xhr) {
                callback(xhr.status);
            }
        });
    }
    

    The reason for that is that you can not return in an AJAX function.

    The code above does not work as desired due to the nature of asynchronous programming. The provided success handler is not invoked immediately, but rather at some time in the future when the response is received from the server. So when we use the 'status' variable immediately after the $.ajax call, its value is still undefined.

提交回复
热议问题