你沒有提到你正在使用的服務器端代碼。當在服務器端使用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 + "\"}");
}
}
}
如果fiddler顯示發出HTTP請求,這意味着緩存沒有發揮作用。 – 2009-12-23 13:36:38
考慮到這種情況,如果我使用POST或者我刪除了'cache:true',並且我無法控制服務器端編碼,那麼我的ajax調用將不起作用。 – 2009-12-23 13:38:31
您能詳細說明爲什麼POST和/或緩存使您的AJAX調用不起作用嗎? – 2009-12-23 13:42:35