在 $.getJSON 中发送之前 [英] beforeSend in $.getJSON
问题描述
如何在 $.getJSON
(跨域)中使用 beforeSend
回调.
How do I use beforeSend
callback in $.getJSON
(cross domain).
更具体地说 $.getJSON
是对 YQL 服务的调用喜欢
More specifically $.getJSON
is call is to a YQL service
Like
select * from html whereurl="http://www.yahoo.com"
select * from html where url="http://www.yahoo.com"
推荐答案
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
}
这篇关于在 $.getJSON 中发送之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!