jquery ajax请求跨域与jsonp在IE6中不起作用
我制作了一个 jquery 插件,如下所示:
(function($){
$.fn.plugin_name = function(){
var methods = {
getSomeThing: function(callback){
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://thirdparty.com/get',
success: function(response){
callback(response);
}
});
},
getDetail: function(callback){
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://thirdparty.com/getdetail',
data:{id:1},
success: function(response){
//this code block has never executed in IE6
},
error: function(request, status, error){
alert(request.statusText); // i get "success" in IE6
alert(status); // i get "parseerror" in IE6
alert(error.error); // i get [object Error] in IE6
}
});
}
};
return this.each(function(){
var self = $(this);
methods.getSomeThing(function(response){ // work OK
//process response
self.html(response.html); // work OK
self.append('<a href="javascript:;" id="linkDetail">View Detail</a>');
self.on('click','#linkDetail',function(){
//i make an ajax request here
methods.getDetail(function(response){
//failed!!
});
});
});
});
}
})(jQuery);
我使用 jsonp 跨域发出请求。
它在 Firefox、Chrome、IE7,8 上运行良好,但在 IE6 上则不然。
方法 getDetail 有问题,当我单击查看详细信息链接时,出现错误。
我尝试使用 Fiddler 对其进行调试,然后当我单击查看详细信息时,我没有看到任何请求,但我仍然收到错误?!
我无法解决这个问题,请有人帮助我!太感谢了!
I make a jquery plugin look like:
(function($){
$.fn.plugin_name = function(){
var methods = {
getSomeThing: function(callback){
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://thirdparty.com/get',
success: function(response){
callback(response);
}
});
},
getDetail: function(callback){
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://thirdparty.com/getdetail',
data:{id:1},
success: function(response){
//this code block has never executed in IE6
},
error: function(request, status, error){
alert(request.statusText); // i get "success" in IE6
alert(status); // i get "parseerror" in IE6
alert(error.error); // i get [object Error] in IE6
}
});
}
};
return this.each(function(){
var self = $(this);
methods.getSomeThing(function(response){ // work OK
//process response
self.html(response.html); // work OK
self.append('<a href="javascript:;" id="linkDetail">View Detail</a>');
self.on('click','#linkDetail',function(){
//i make an ajax request here
methods.getDetail(function(response){
//failed!!
});
});
});
});
}
})(jQuery);
I used jsonp to making requests across domains.
It works fine on Firefox, Chrome, IE7,8 but no luck on IE6.
There is something wrong at method getDetail, when i click View Detail link, i got errors.
I try to debug it with Fiddler, then i don't see any request when i click View Detail, but i still got the errors ?!
I can not figure this problem, someone help me, please! Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了问题!我只是想与您分享这个技巧。
当像这样的锚点上触发事件单击时,似乎出现了问题:
在这种情况下我使用 event.preventDefault() ,因此不会触发事件的默认操作。
然后在 IE6 中一切正常!
I solved the problem! I just want to share with you this tip.
Seem there is something wrong when event click was fired on an anchor like this:
I use event.preventDefault() in this case, so the default action of the event will not be triggered.
Then everything work fine in IE6!