Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

javascript - Ajax async false is deprecated?

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 doesn't work:

$("#form").submit(function(e) {
var cnf;

$.ajax({
    type: "POST", 
    url: 'page.php', 
    data: $('#form').serialize(),
    async: true,
    success: function(responseText) { 
        if(responseText.indexOf('err') != -1) {
            cnf = "error";
        } 
        else {
            cnf = "success";
        }
        return callBack( cnf );
    }, 
    error: function() {
        cnf = "error";  
        return callBack( cnf ); 
    } 
});


if(cnf == "success")
{
   alert('ok');
}
});

The HTML:

<form id="form">
 <input type="text" name="email">
 <input type="submit">
</form>

If I use async: false this works. Using callBack I see the solution here: wait for a jquery ajax callback from calling function

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the documentation:

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated

You are not using it with jqXHR.

That said, non-async HTTP is horrible with JavaScript and best avoided. Do your work in or from the callback. Don't try to return data to the calling function so it can do the work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...