叼花硬汉 2011-07-13 20:31 采纳率: 0%
浏览 301
已采纳

执行同步 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条回答 默认 最新

  • 7*4 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 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败