jquery ajax请求跨域与jsonp在IE6中不起作用

发布于 2025-01-01 03:29:39 字数 1919 浏览 0 评论 0原文

我制作了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2025-01-08 03:29:39

我解决了问题!我只是想与您分享这个技巧。

当像这样的锚点上触发事件单击时,似乎出现了问题:

<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 中一切正常!

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:

<a href="javascript:;" id="linkDetail">View Detail</a>

I use event.preventDefault() in this case, so the default action of the event will not be triggered.

self.on('click','#linkDetail',function(event){
        event.preventDefault();
            //i make an ajax request here
            methods.getDetail(function(response){
                //failed!!
            });
        });

Then everything work fine in IE6!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文