Ajax async false is deprecated?

前端 未结 2 1821
忘了有多久
忘了有多久 2020-12-21 07:05

I am using jQuery 1.7 and I use async:false for my AJAX requets, but I\'ve learned that this function is deprecated.

I need to use callback but this doe

2条回答
  •  不思量自难忘°
    2020-12-21 07:25

    Continuing from the comments: You seem to be a little obsessed with callbacks, when you don't seem to need any! :)

    The simple change to your existing code is to throw away cnf and simply put your code in the success part of the ajax call:

    $("#form").submit(function (e) {
        $.ajax({
            type: "POST",
            url: 'page.php',
            data: $('#form').serialize(),
            async: true,
            success: function (responseText) {
                if (responseText.indexOf('err') != -1) {
                    // Do something when it fails here
                } else {
                    // Do something when it succeeds here!!!!
                    alert('ok');
                    // e.g. move on to "step 2"
                }
            },
            error: function () {
               // Do something when it fails here
            }
        });
    });
    

提交回复
热议问题