ie6下jsonp返回失效问题

来源:luqidong 发布时间:2014-03-06 22:05:27 点击数:

问题描述:

在ie6下用jsop,用fillder查看的确是已经发送成功并且已经有返回了,状态是成功的。但是成功函数就是不执行。

<code class="lang-javascript">(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('&lt;a href="javascript:;" id="linkDetail"&gt;View Detail&lt;/a&gt;');
 
            self.on('click','#linkDetail',function(){
                //i make an ajax request here
                methods.getDetail(function(response){
                    //failed!!
                });
            });
 
        });
    });
}
</code>

解决方法:

在元素click的时候写下event.preventDefault();
例如:

<code class="lang-javascript">self.on('click','#linkDetail',function(event){
        event.preventDefault();
            //i make an ajax request here
            methods.getDetail(function(response){
                //failed!!
            });
        });
</code>

ps:ie6下会因为event.preventDefault();报错,只要写成event.returnValue = false;就好。

本文由免费oa系统78oa原创,转载请注明出处。