有什么方法可以等待AJAX​​响应并停止执行吗? [英] Is there any way to wait for AJAX response and halt execution?

查看:57
本文介绍了有什么方法可以等待AJAX​​响应并停止执行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想执行的一些代码.我想等待AJAX​​响应,以便可以从服务器返回一些信息.有什么办法做到这一点?

Here is some code I'd like to execute. I'd like to wait for AJAX response so I can return something from the server. Any way to achieve this?

function functABC(){
    $.ajax({
        url: 'myPage.php',
        data: {id: id},
        success: function(data) {
            return data;
        }
    });

    //Wait for AJAX (???)
}

var response = functABC();

推荐答案

所有Ajax调用可以异步(使用回调函数,这是在成功"键之后指定的函数)或同步完成-有效地阻止和等待服务器的答案. 要获得同步执行,您必须指定

All Ajax calls can be done either asynchronously (with a callback function, this would be the function specified after the 'success' key) or synchronously - effectively blocking and waiting for the servers answer. To get a synchronous execution you have to specify

async: false 

此处

但是请注意,在大多数情况下,异步执行(通过成功的回调)就可以了.

Note, however, that in most cases asynchronous execution (via callback on success) is just fine.

这篇关于有什么方法可以等待AJAX​​响应并停止执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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