IE7 和 IE6 中的 jQuery .ajax 方法不起作用,但在 Firefox 中工作正常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2320090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:14:25  来源:igfitidea点击:

jQuery .ajax method in IE7 & IE6 not working but working fine in Firefox

jqueryajaxinternet-explorer-7internet-explorer-6

提问by RyanP13

This relates to my previous post:

这与我之前的帖子有关:

jQuery .load Method causing page refresh AJAX

jQuery .load 方法导致页面刷新 AJAX

I changed my implmentation to use the .ajax method instead of .load and it works fine in Firefox but not in IE7 or IE6:

我更改了我的实现以使用 .ajax 方法而不是 .load 并且它在 Firefox 中运行良好,但在 IE7 或 IE6 中不起作用:

    $('ul#coverTabs > li > a').live('click', function(event) {

    // Find href of current tab
    var $tabValue = $(this).attr('href');

    $.ajax({
        type: "GET",
        cache: false,
        dataType: "html",
        url: $(this).attr('href'),
        success: function(data){

        $(data).find('.benefitWrap').each(function(){

            var $benefitWrap = $(this).html();

            $('.benefitWrap').replaceWith($('<div class="benefitWrap">' + $benefitWrap + '</div>'));

        });

       }

    });

    event.preventDefault(); 

});

This is killing me as it has taken ages to get this far.

这让我很伤心,因为花了很长时间才走到这一步。

Any ideas where i am going wrong?

我哪里出错了?

回答by Corey Downie

Just an quick though. I have had some frustrating issues with jQuery in the past that silently failed with IE6/7. In almost all cases, somewhere in my code (not necessarily in the ajax call in question) I had a extra comma after an argument like this:

只是很快。过去我在使用 jQuery 时遇到了一些令人沮丧的问题,这些问题在 IE6/7 中默默地失败了。在几乎所有情况下,在我的代码中的某个地方(不一定在有问题的 ajax 调用中),我在这样的参数后有一个额外的逗号:

$.ajax({
        type: "GET",
        cache: false,
        dataType: "html",
        url: $(this).attr('href'),
        success: function(){},
)}

I would run the script through jslintto see if there is anything funny in the syntax causing this issue before doing proper debugging.

在进行适当的调试之前,我会通过jslint运行脚本,以查看导致此问题的语法中是否有任何有趣的地方。

回答by RyanP13

I accidentally worked out what the issue was.

我不小心找出了问题所在。

The link referenced in this variable:

此变量中引用的链接:

var $tabValue = $(this).attr('href');

Had a hash value on the end like so:

最后有一个哈希值,如下所示:

https://bupacouk.bwa.local.internal.bupa.co.uk/cash-plan-quote/quoteAction.do?getBenefitLevelDetails=getBenefitLevelDetails&productPolicyId=7850#a1

This cause the AJAX to fall over in bothe versions of IE.

这会导致 AJAX 在两个版本的 IE 中失败。

Using the following code:

使用以下代码:

var $withoutHash = $tabValue.substr(0,$tabValue.indexOf('#'));

Getrs rid of it and it now works :)

摆脱它,它现在可以工作了:)

回答by Dave Ward

eventis a reserved word in some versions of IE. Try changing the parameter you're capturing from eventto something sure to avoid collision, like evt, e.g.:

event在某些版本的 IE 中保留字。尝试将您正在捕获的参数更改为event确保避免碰撞的参数evt,例如:

$('ul#coverTabs > li > a').live('click', function(evt) {

  evt.preventDefault();

  // Find href of current tab
  var $tabValue = $(this).attr('href');

  $.ajax({
    type: "GET",
    cache: false,
    dataType: "html",
    url: $(this).attr('href'),
    success: function(data){

    $(data).find('.benefitWrap').each(function(){

        var $benefitWrap = $(this).html();

        $('.benefitWrap').replaceWith($('<div class="benefitWrap">' + $benefitWrap + '</div>'));

    });

   }

});

Update

更新

Upon further review, I believe your problem is the find(). In this case, you should use filter().

经过进一步,我相信您的问题是find(). 在这种情况下,您应该使用filter().

success: function(data) {
  $(data).filter('.benefitWrap').each(function() {
    // This should accomplish the same thing a bit more cleanly.
    $('.benefitWrap').html(this.innerHTML);
  });
}

That can be further refactored down to just:

这可以进一步重构为:

success: function(data) {
  $('.benefitWrap').html($(data).filter('.benefitWrap').html());
}

回答by oori

Hash in the url is an issue with IE 6 and 7, xhr.status eror 500. Resolved well with:

url 中的哈希是 IE 6 和 7 的问题,xhr.status 错误 500。很好地解决了:

function removeHash(url) { return url.replace(/#(.*)$/, ""); }
$.get(removeHash(this.href),...)

see: http://forum.jquery.com/topic/ticket-3808-ajax-load-fails-404-if-there-is-a-hash-in-the-url

见:http: //forum.jquery.com/topic/ticket-3808-ajax-load-fails-404-if-there-is-a-hash-in-the-url

回答by RyanP13

Well the problem seems to be here:

那么问题似乎在这里:

success: function(data){    
            alert(data);
            $(data).find('.benefitWrap').each(function(){
                alert(data);
                var $benefitWrap = $(this).html();
                $('.benefitWrap').replaceWith($('<div class="benefitWrap">' + $benefitWrap + '</div>'));                
        });

The second alert never appears but the first does. I hate IE!

第二个警报永远不会出现,但第一个会出现。我讨厌IE!