代码之家  ›  专栏  ›  技术社区  ›  Soufiane Hassou

$.Ajax在IE6上工作不正常

  •  3
  • Soufiane Hassou  · 技术社区  · 15 年前

    基本上,我有这样的东西:

    $.ajax({
        type: "GET",
        url: "my_url",
                cache: true,                
                success: function(data) {
                  /* code here */
                },
            dataType: 'json'
    }); 
    

    此代码在所有测试过的浏览器(IE7/8、Chrome、Safari、Firefox)中都有效,但在IE6中,不会调用Success函数。

    我用fiddler来查看HTTP请求中发生了什么,一切看起来都很正常,我得到了预期的结果作为HTTP答案,但是在IE6中似乎没有调用success,对于onerror也是如此。

    有什么想法吗?

    4 回复  |  直到 15 年前
        1
  •  1
  •   Luca Matteis    15 年前

    你确定这不仅仅是缓存吗?删除浏览器缓存并再次测试。

    一个好的测试用例是去掉“cache”选项,并将其作为post请求(因为get-ajax调用总是缓存在ie6中)。

        2
  •  2
  •   Josh Stodola    15 年前

    试用使用 complete 而不是 success . 如果它持续触发,那么您可以评估状态代码以确定它是否成功…

    $.ajax({
      type: "GET",
      cache: true,
      complete: function(xhr) {
        if(xhr.status != 200) {
          throw "Error!";
          return;
        }
    
        var data = xhr.responseText;
      }
    });
    
        3
  •  1
  •   Milan Gardian    15 年前

    您没有提到正在使用的服务器端代码。在服务器端使用ASP.NET(ASHX处理程序)时,我在IE中对jquery-ajax调用有一些问题。在开始写响应之前,当我完全阅读请求时,它们就消失了(即使在我的案例中,我使用的是post,而不是get请求,因此请求的主体包含一些数据)。

    我编写了以下简单的ASP.NET项目来测试IE6中的问题。但是,我无法复制(IE6SP2在虚拟机中运行,命中了IIs7.5,会正确显示来自成功处理程序的警报框)。你能试着在你的环境中运行它并报告它是否在IE6中为你工作吗?

    注释 :有时,当我清除ie6缓存并注释掉ashx.cs中的“setcacheability”行时,第一个单击“send”按钮不会显示成功警报框,但随后的单击确实会显示成功警报框。也许您所需要的只是在实现中向调用响应添加“无缓存”头?

    文件 索引文件

    <!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>
    

    文件 阿希克斯

    <%@ WebHandler Language="C#" CodeBehind="Api.ashx.cs" Class="AjaxTest.Api" %>
    

    文件 AX.AXX.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 + "\"}");
            }
        }
    }
    
        4
  •  0
  •   Mutation Person    15 年前

    如果您添加了一个失败函数,并警告其中的响应文本,会发生什么?

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