【问题标题】:jQuery AJAX call works in Firefox but not IE - valid response returnedjQuery AJAX 调用在 Firefox 中有效,但在 IE 中无效 - 返回有效响应
【发布时间】:2013-10-31 20:17:04
【问题描述】:

我在 Firefox 中运行 AJAX 调用时遇到了一个奇怪的问题,但在 IE 中却没有。当我添加一个 IE alert() 时,我看到了返回的内容,但它不想使用 jQuery .html() 命令插入。这是我的示例代码:

$(document).on('click','.OpenForm',function(e) {    
    e.preventDefault();
    var FormControl = $(this).attr('id');       //ex: id=FormView_12 or FormEdit_12
        FormControl = FormControl.split('_');
    var FormControlType = FormControl[0];
    var FormID = FormControl[1];
    $.post("./includes/Getform.php", { "t" : "view" , FormID : FormID })  
     .done(function(data) {
      if (data.length>0){
        data = data.replace(/(\r\n|\n|\r)/gm,"");
        //alert(data); ---THIS ALERTS THE RESPONSE IN IE8
        //console.log(data); --- THIS SHOWS RESPONSE IN FF
        $('.ProjectContentLoad').html(data).show();
      }
   })
   .fail( function(xhr, textStatus, errorThrown) {
      error_handling(xhr.responseText);
   });
});

【问题讨论】:

  • 如果删除 console.log() 会发生什么?
  • 不走运,我只是在 IE 中的警报和 FF 中的 console.log 之间交替。应该在最初的帖子中提到我使用的是 IE8。
  • * 如果开发人员工具未打开,IE 会出现console.log(...) 的问题...它将静默崩溃

标签: javascript jquery html ajax internet-explorer


【解决方案1】:

也许您返回的 HTML 无效。 FF 没有问题,但 IE8 肯定有 ;) 也许你忘记关闭一个 div?

以下帖子显示了一些遇到完全相同问题的人:jQuery AJAX GET html data IE8 not working

【讨论】:

  • 看起来很简单,但这就是问题所在——我的
    放错了地方。感谢大家的帮助!
【解决方案2】:

只是在黑暗中拍摄,但 IE 不会被您发送的 JS 对象弄糊涂是吗?

{ "t" : "view" , FormID : FormID }

你有什么理由使用字符串作为“t”的键吗?

【讨论】:

  • 为什么会混淆?这是一个有效的对象。
【解决方案3】:

您的 JS 控制台上是否有任何错误消息?你能展示一下数据内容是什么吗?

我在想两件事:

1) 如果你不在 IE8 中运行开发工具,console.log() 是未定义的。要解决它,请将此代码放在页面顶部:

<script type="text/javascript"> if (!window.console) console = {log: function() {}}; </script>

来源:'console' is undefined error for Internet Explorer

2) 可能是 IE 的 AJAX 缓存问题(我认为不是因为您使用的是 post() ),请尝试将此代码放在页面顶部:

$.ajaxSetup({ cache: false });

来源:How to prevent a jQuery Ajax request from caching in Internet Explorer?

【讨论】:

  • 把它分成两部分是一个不好的建议。
  • 哦,是的,你是对的,.html( htmlString ) 返回一个 JQuery 对象。 api.jquery.com/html/#html2,我编辑我的消息。
  • 将其分成两部分并不能解决问题。还添加了没有运气的 cache:false 命令,检查以确保 HTML 现在有效...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2011-12-17
  • 2010-11-24
  • 2011-02-18
  • 2011-01-15
  • 1970-01-01
相关资源
最近更新 更多