【问题标题】:jQuery.ajax() - undefined data returned in IE9jQuery.ajax() - IE9 中返回的未定义数据
【发布时间】:2012-06-25 11:18:56
【问题描述】:

我有一个非常简单的代码:

$.ajax({
  cache: false,
  dataType: 'html',
  complete: function(jqXHR){
    console.log(jqXHR.responseText);
  },
  success: function(data){
    console.log(data);
  },
  url: 'http://follows.pl/pages/ajaxtest'
});

它在 ff、chrome 和 IE8 中返回一些文本,但在 IE9 中它显示两次“未定义”。

我查看了 IE9 中的开发人员工具,它显示正常响应,因此请求工作正常,响应正常,但变量未定义

响应标题:

Response    HTTP/1.1 200 OK
Cache-Control   no-cache
Content-Type    text/html; charset: UTF-8
Pragma  no-cache

回复

string(4) "test"

【问题讨论】:

标签: jquery ajax internet-explorer-9


【解决方案1】:

我怀疑这是你的问题:

Content-Type    text/html; charset: UTF-8

该值的格式不正确(字符集后的 ':' 是错误的)并且 IE9 不喜欢它,而是默默地失败而不是说出有用的东西。试试这个:

Content-Type:    text/html;charset=utf-8

【讨论】:

    【解决方案2】:

    我尝试了一切来解决在 IE 浏览器上发布 ajax 的问题(例如,添加到 jquery ajax 对象中没有缓存、dataType、configType 等...),但最终问题不在 ajax/javascript 中,而是在PHP 文件:仅对于 IE 浏览器,PHP 文件必须以以下标头开头

    header("Content-type: text/html; charset=utf-8");
    

    因此,您必须明确指出您通过 ajax 调用获得的 php 页面的内容类型

    例如,假设您在其中放置了一个名为 one.html 的 html 页面和一个名为 two.php

    的 php 页面

    在 one.html 中设置 javascript 为

    var url = 'two.php';
    $.ajax({
    url: url,
    type: "POST",
    success: function(response){
    alert(response)
    }
    });
    

    在两个.php页面中设置如下:

    <?php
    header("Content-type: text/html; charset=utf-8");
    echo ('stuff to do');
    ?>
    

    以这种方式对我来说就像一个魅力!

    【讨论】:

      【解决方案3】:

      试试这个:

      $.ajax({
        cache: false,
        dataType: 'html',
        complete: function(data){
          console.log(data);
        },
        success: function(data){
          console.log(data);
        },
        url: 'http://follows.pl/pages/ajaxtest'
      });
      

      通知

      在成功函数中

       success: function (data, textStatus, jqXHR)
      

      对象是third 参数。

      您实际上是通过访问数据中不存在的属性来响应数据。

      也在完整的功能中

       complete: function (jqXHR, complete_textStatus)
      

      这里的对象是first 地点!

      你必须记住位置。

      【讨论】:

      • 问题是成功后我应该有任何数据,但我没有。
      猜你喜欢
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多