问题描述
如何在 $。getJSON
(跨网域)中使用 beforeSend
回调。
How do I use beforeSend
callback in $.getJSON
(cross domain).
更具体地 $。getJSON
是对YQL服务的调用
Like
More specifically $.getJSON
is call is to a YQL service Like
推荐答案
beforeSend 的目的是获取原始XHR对象(通常用于设置HTTP头)。你不需要它来启动纺纱器等。此代码(来自@petersendidit):
The only purpose of beforeSend is to get at the raw XHR object (normally for setting HTTP headers on it). You don't need it for kicking off spinners and the like. This code here (from @petersendidit):
jQuery.ajax({
url: url,
dataType: "json",
beforeSend: function(){
$('.loading').show();
}
});
最好这样写:
$('.loading').show();
jQuery.ajax({
url: url,
dataType: "json"
});
这意味着,除非你需要jQuery.ajax中的任何高级选项,否则你原来计划使用jQuery。 getJSON就好了。所以你说你想显示一个加载GIF,只是这样做,忘了 beforeSend
。
Which means, unless you need any advanced options in jQuery.ajax, your original plan to use jQuery.getJSON is just fine. So you say you want to show a loading GIF, just do this and forget about beforeSend
.
jQuery(".someSpinnerImage").show();
jQuery.getJSON("http://www.somedomain.com/someurl", function(data) {
jQuery(".someSpinnerImage").hide();
// Do something with data
}
这篇关于beforeSend在$ .getJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!