簡體   English   中英

$ .ajax在IE6上無法正常工作

[英]$.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'});

http://docs.jquery.com/Ajax/jQuery.ajax#options

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM