jsonp 没有在发送之前触发?
我正在开发一个项目,使用 $.ajax 并将 dataType 设置为 jsonp 从不同域调用 Web 服务。
$.ajax({
type: "GET",
url: testService.asmx,
async: true,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
beforeSend: function (XMLHttpRequest) {
alert('Before Send'); //Nothing happnes
},
success: function (response) {
alert('Success'); //this was fired
},
complete: function (XMLHttpRequest, textStatus) {
alert('After Send'); //this was fired
}
});
问题是我有一个...正在加载动画,我想在处理 Web 服务请求时显示该动画。我尝试使用“beforeSend:”来显示加载动画,但似乎“beforeSend”没有被解雇。
当应用程序位于同一域(使用 jsonp)时,动画工作正常,但是当我将应用程序移动到不同的服务器时,除了“beforeSend”没有被调用之外,一切正常。所以用户将无法看到加载动画。
有什么解决方法吗?
I am working on a project to call a webservice from different domain using $.ajax with dataType set to jsonp.
$.ajax({
type: "GET",
url: testService.asmx,
async: true,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
beforeSend: function (XMLHttpRequest) {
alert('Before Send'); //Nothing happnes
},
success: function (response) {
alert('Success'); //this was fired
},
complete: function (XMLHttpRequest, textStatus) {
alert('After Send'); //this was fired
}
});
The problem is I have a ...loading animation which I want to display while the web service request is being processed. I tried using "beforeSend:" to show the loading animation but it seems like "beforeSend" is not getting fired.
The animation works fine when the app is on the same domain (using jsonp) but when I move the app into a different server, everything works except "beforeSend" is not getting called. So users will not be able to see the loading animation.
Is there any workaround for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
跨域JSONP请求不使用XMLHTTPRequest,因此事件流程不同。
您可以在调用 $.ajax 后立即显示加载动画。
Cross-domain JSONP requests do not use XMLHTTPRequest, so the event flow is different.
You can simply show your loading animation right after calling $.ajax.
jQuery JSONP 的
beforeSend
实现仍在进行中。目前的解决方案是,
在
$.ajax({...});
之前使用:beforeSend
implemention for jQuery JSONP is sill in progress.The solution for now, use:
before
$.ajax({...});
.