如何使用jQuery执行同步请求? [英] How can I do a synchronous request with jQuery?

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

问题描述

为什么不将该函数返回给responseText?

Why don't return that function the responseText?

function LoadBookmarksAsXml()
{
  return $.ajax(
  {
    type: 'GET',
    async: false,
    url:  'http://www.google.com/bookmarks/?output=xml&num=10000'
  }).responseText;
}

(如果我定义成功回调函数并将async设置为真的!)
提前致谢!!

(It works if I define a success-callback-function and set async to true!) Thanks in advance!!

编辑:不要担心跨域通话; user603003表示(在对已删除的答案的评论中),这是在允许跨域请求的Chrome扩展程序中。

Edit: Don't worry about the cross-domain call; user603003 says (in a comment on a now-deleted answer) that this is in a Chrome extension where cross-domain requests are allowed.

解决方案,如果有人想要这样做:

The solution if someone wants to do the same:

return $.ajax(
{
  type: 'GET',
  async: false,
  url:  'http://www.google.com/bookmarks/?output=xml&num=10000',
});

(您将获得一个XMLHTTPRequest对象。)

(You will get a XMLHTTPRequest object.)

推荐答案

我没有立即看到为什么它没有返回它,但我仍然使用成功回调:

I'm not immediately seeing why it's not returning it, but I'd still use a success callback:

function LoadBookmarksAsXml()
{
  var result;
  $.ajax(
  {
    type: 'GET',
    async: false,
    url:  'http://www.google.com/bookmarks/?output=xml&num=10000',
    success: function(data) {
        result = data;
    }
  });
  return result;
}

即使 $。ajax 返回 XMLHttpRequest 对象(在1.4或更早版本中)或 jqXHR 对象(在1.5以上),我' d为了清楚起见,仍然更喜欢使用 success 函数和错误函数。此外,不同版本的jQuery在错误时为 responseText 提供不同的值(至少在Chrome上; 1.4.4返回空字符串,1.5.0返回未定义)。

Even though $.ajax returns an XMLHttpRequest object (in 1.4 or earlier) or a jqXHR object (in 1.5+), I'd still prefer using a success function and an error function for clarity. Also, different versions of jQuery give you different values for responseText on error (at least on Chrome; 1.4.4 returns an empty string, 1.5.0 returns undefined).

如果以任何方式可以避免

If there's any way you can avoid it, avoid it. Synchronous requests completely lock up the UI of most browsers (not just your page's UI, every page in every tab that browser is managing). Since ajax requests can take a second or two (or five, or ten), this makes for a very unpleasant user experience. Nearly all the time, you can avoid it by refactoring your function so it accepts a callback to use to supply the result:

function LoadBookmarksAsXml(callback)
{
  $.ajax(
  {
    type: 'GET',
    url:  'http://www.google.com/bookmarks/?output=xml&num=10000',
    success: function(data) {
        callback(data);
    },
    error: function() {
        callback(null);
    }
  });
}






关闭主题:如果请求完全正常,我会感到惊讶,因为从表面上来看(除非您为Google工作),由于同源政策。解决SOP的各种方法:


Off-topic: I'll be surprised if the request works at all, though, because on the face of it (unless you work for Google), that request will fail because of the Same Origin Policy. Various ways to get around the SOP:

  • JSONP
  • CORS (but it requires browser support and that www.google.com allow the request from your origin)
  • Using YQL as a proxy

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

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