YaoRaoLov 2011-07-13 20:31 采纳率: 50%
浏览 305
已采纳

执行同步 AJAX 请求

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;

}

转载于:https://stackoverflow.com/questions/6685249/jquery-performing-synchronous-ajax-requests

  • 写回答

5条回答 默认 最新

  • Didn"t forge 2011-07-13 20:35
    关注

    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

    PLEASE NOTE: Setting async property to false is deprecated and in the process of being removed (link). Many browsers including Firefox and Chrome have already started to print a warning in the console if you use this:

    Chrome:

    Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

    Firefox:

    Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user’s experience. For more help http://xhr.spec.whatwg.org/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大