jQuery的:执行同步AJAX请求 [英] jQuery: Performing synchronous AJAX requests

查看:151
本文介绍了jQuery的:执行同步AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在过去做了一些jQuery的,但我完全停留在此。我知道的优点和使用同步Ajax调用的利弊,但在这里它是必需的。

远程页面加载(控制与萤火虫),只是再也回不去显示。

我应该怎么做不同的,使我的函数正确返回?

 函数getRemote(){

    VAR遥控;

    $阿贾克斯({
        键入:GET,
        网址:remote_url,
        异步:假的,
        成功:功能(数据){
            远程=数据;
        }
    });

    返回远端;

}
 

解决方案

当你正在做一个同步请求,这应该是

 函数getRemote(){
    返回$阿贾克斯({
        键入:GET,
        网址:remote_url,
        异步:假的
    })responseText的;
}
 

示例 - <一个href="http://api.jquery.com/jQuery.ajax/#example-3">http://api.jquery.com/jQuery.ajax/#example-3

I've done some jQuery in the past, but I am completely stuck on this. I know about the pros and cons of using synchronous ajax calls, but here it will be required.

The remote page is loaded (controlled with firebug), but no return is shown.

What should I do different to make my function to return properly?

function getRemote() {

    var remote;

    $.ajax({
        type: "GET",
        url: remote_url,
        async: false,
        success : function(data) {
            remote = data;
        }
    });

    return remote;

}

解决方案

As you're making a synchronous request, that should be

function getRemote() {
    return $.ajax({
        type: "GET",
        url: remote_url,
        async: false
    }).responseText;
}

Example - http://api.jquery.com/jQuery.ajax/#example-3

这篇关于jQuery的:执行同步AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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