[英]jquery ajax request cross domain with jsonp does not work in 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对其进行调试,然后单击“ 查看详细信息”时没有看到任何请求,但仍然出现错误?
我想不出这个问题,请有人帮我! 非常感谢!
我解决了问题! 我只想与您分享这个技巧。
像这样在锚点上触发事件点击时,似乎出了点问题:
<a href="javascript:;" id="linkDetail">View Detail</a>
在这种情况下,我使用event.preventDefault(),因此不会触发事件的默认操作。
self.on('click','#linkDetail',function(event){
event.preventDefault();
//i make an ajax request here
methods.getDetail(function(response){
//failed!!
});
});
然后,一切都可以在IE6中正常工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.