Ajax的异步虚实德precated? [英] Ajax async false is deprecated?

查看:128
本文介绍了Ajax的异步虚实德precated?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

荫使用jQuery 1.7和我用我的AJAX requete 异步:假但我学会在互联网这个功能是去precated我需要使用回调,但不工作

Iam use jQuery 1.7 and i use for my ajax requete async:false but i learn in internet this function is deprecated i need to use callback but not 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');
}
});

在HTML

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

如果我使用异步:假所有的好,回调这里我<看到了一个解决方案href=\"http://stackoverflow.com/questions/11576176/wait-for-a-jquery-ajax-callback-from-calling-function\">wait从调用一个jQuery AJAX回调函数

If i use async: false all its good, for callBack i see the solution here wait for a jquery ajax callback from calling function

感谢您

推荐答案

从注​​释继续:你似乎是用的回调的,有点痴迷,当你似乎并不需要任何! :)

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

到现有code的简单变化是扔掉 CNF ,简单地把你的code在Ajax调用成功的一部分:

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
        }
    });
});

这篇关于Ajax的异步虚实德precated?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆