如何让程序等待ajax结果继续处理? [英] How to let program wait for ajax result to continue processing?

查看:264
本文介绍了如何让程序等待ajax结果继续处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做表单验证。在窗体检查功能中,我调用了一个ajax函数来验证字段。如果ajax的返回数据等于否,则此函数应返回false。但是,它从不等待ajax结果返回。我应该如何处理这个问题?
我的按钮代码:

I'm doing form validation. In the form check function, I called an ajax function to verify a field. if the return data of ajax equals "No", this function supposed to return false. However, it never wait for the ajax result return. How should I handle this issue? my button code:

jQuery代码:

function Checkform() {
        var result = true;
        $('input:password').each(function() {
            if ($(this).val() == "") {
                $(this).focus();
                $(this).addClass('HighlightBorder');
                alert("Please set password.");
                result = false;
                return false;
            }
        });
        if (!result)
            return false;
......
       $.post(rootPath + "/AjaxPages/SearchAddress.aspx", { "street": street, "city": city, "state": state, "zip": zip, "type": "DPV" }, function(data) {
            if (data == "No") {
                alert("Invalid company address, did you type in a wrong address?");
                result=false;
            } 
        });
        return result;
}

然后我将我的ajax代码修改为

Then I modified my ajax code to

$.post(rootPath + "/AjaxPages/SearchAddress.aspx", { "street": street, "city": city, "state": state, "zip": zip, "type": "DPV" }, function(data) {
            if (data == "No") {
                alert("Invalid company address, did you type in a wrong address?");
                return false;
            } else
                return true;


$ b b

我认为这次返回值是在数据传出后执行的,但仍然不起作用。

I think this time the return value is performed after the data comes out, but still, it doesn't work.

推荐答案

ajax是异步的,因此您必须使用回调。

ajax is asynchronous, so you have to use callbacks.

   $.post(rootPath + "/AjaxPages/SearchAddress.aspx", { "street": street, "city": city, "state": state, "zip": zip, "type": "DPV" }, function(data) {
        if (data == "No") {
            alert("Invalid company address, did you type in a wrong address?");
            some_callback_function();//callback
        } 
    });

这篇关于如何让程序等待ajax结果继续处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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