8

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

$.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"
4

3 回答 3

7

我怀疑这是你的问题:

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

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

Content-Type:    text/html;charset=utf-8
于 2013-04-03T13:07:08.537 回答
1

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

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

所以,你必须明确指出内容类型您通过 ajax 调用获得的 php 页面的内容类型。

例如,假设一个名为one.html的 html 页面放置您的 javascript 和一个名为的 php 页面的 html 页面和一个名为two.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');
?>

这样对我来说,它就像一个魅力!

于 2013-04-29T13:09:59.343 回答
0

尝试这个 :

$.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地方!

你必须记住地点。

于 2012-06-23T09:31:31.493 回答