[英]$.ajax not working properly on IE6
基本上,我有这样的事情:
$.ajax({
type: "GET",
url: "my_url",
cache: true,
success: function(data) {
/* code here */
},
dataType: 'json'
});
此代码适用于所有经过测试的浏览器(IE7 / 8,chrome,safari,firefox),但在IE6中,不会调用成功函数。
我使用Fiddler来查看HTTP请求中发生了什么,一切看起来都很正常,我得到了预期的结果作为HTTP答案但是在IE6中似乎没有成功调用,对于onerror也是如此。
有什么想法吗?
尝试使用complete
而不是success
。 如果它一直触发,那么你可以评估状态代码以确定它是否成功...
$.ajax({
type: "GET",
cache: true,
complete: function(xhr) {
if(xhr.status != 200) {
throw "Error!";
return;
}
var data = xhr.responseText;
}
});
你确定它不只是一个缓存的东西吗? 删除浏览器缓存并再次测试。
一个好的测试用例是摆脱'cache'选项,并将其作为POST请求(因为GET ajax调用总是在ie6中缓存)。
您没有提到您正在使用的服务器端代码。 当在服务器端使用ASP.NET(ashx处理程序)时,我在IE中遇到了一些jQuery AJAX调用问题。 当我在开始编写响应之前完全读取请求时它们就消失了(即使在我的情况下我使用的是POST,而不是GET请求,因此请求的主体包含一些数据)。
我编写了以下简单的ASP.NET项目来测试IE6中的问题。 但是我无法重现(在虚拟机中运行的IE6 SP2命中IIS 7.5正确地显示了成功处理程序的警报框)。 您可以尝试在您的环境中运行它并报告它是否适用于您的IE6?
注意 :有时当我清除IE6缓存并注释掉ashx.cs中的“SetCacheability”行时,第一次单击“发送”按钮将不会显示成功警告框,尽管后续点击确实显示了它。 也许您只需要在实现中为调用响应添加“no-cache”标头?
文件index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX GET test</title>
</head>
<body>
<input type="button" id="test" value="Send" />
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$("#test").click(function () {
$.ajax({
url: "Api.ashx?param=one",
cache: true,
type: "GET",
dataType: "json",
success: function (data) {
alert("Success, result = " + data.result);
},
error: function (request, status, err) {
alert("Error");
}
});
});
</script>
</body>
</html>
文件Api.ashx
<%@ WebHandler Language="C#" CodeBehind="Api.ashx.cs" Class="AjaxTest.Api" %>
文件Api.ashx.cs
using System.Diagnostics;
using System.Text;
using System.Web;
namespace AjaxTest
{
public class Api : IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context)
{
var param = context.Request["param"]; // this flushes the request
Trace.WriteLine("Request: \"" + context.Request.RawUrl + "\", param: \"" + param + "\"", "** Debug");
context.Response.StatusCode = 200;
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Write("{\"result\":\"" + param + "\"}");
}
}
}
如果添加失败函数会发生什么,并在那里警告responseText?
$.ajax({
type: "GET",
url: "my_url",
cache: true,
success: function(data) {
/* code here */
},
error: function(data) {
alert(data.responseText);
},
dataType: 'json'});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.