简体   繁体   English

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

[英]$.ajax not working properly on IE6

Basically, I have something like this: 基本上,我有这样的事情:

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

This code is working in all tested browsers (IE7/8, chrome, safari, firefox) but in IE6 the success function is not called. 此代码适用于所有经过测试的浏览器(IE7 / 8,chrome,safari,firefox),但在IE6中,不会调用成功函数。

I used Fiddler to see what's going on in the HTTP requests, and everything seems normal, I get the expected result as an HTTP answer but success doesn't seem to be called in IE6, same for onerror. 我使用Fiddler来查看HTTP请求中发生了什么,一切看起来都很正常,我得到了预期的结果作为HTTP答案但是在IE6中似乎没有成功调用,对于onerror也是如此。

Any thoughts? 有什么想法吗?

Try using complete instead of success . 尝试使用complete而不是success If it fires consistently then you can evaluate the status code to determine if it was successful... 如果它一直触发,那么你可以评估状态代码以确定它是否成功...

$.ajax({
  type: "GET",
  cache: true,
  complete: function(xhr) {
    if(xhr.status != 200) {
      throw "Error!";
      return;
    }

    var data = xhr.responseText;
  }
});

Are you sure it's not just a cache thing? 你确定它不只是一个缓存的东西吗? Remove your browsers cache and test again. 删除浏览器缓存并再次测试。

A good test case would be getting rid of the 'cache' option, and also making it a POST request (since GET ajax calls are always cached in ie6). 一个好的测试用例是摆脱'cache'选项,并将其作为POST请求(因为GET ajax调用总是在ie6中缓存)。

You don't mention the server-side code you're using. 您没有提到您正在使用的服务器端代码。 I had some issues with jQuery AJAX calls in IE when using ASP.NET on the server side (a ashx handler). 当在服务器端使用ASP.NET(ashx处理程序)时,我在IE中遇到了一些jQuery AJAX调用问题。 They went away when I read the request fully before starting to write the response (even though in my case I was using POST, not GET request, so the body of the request contained some data). 当我在开始编写响应之前完全读取请求时它们就消失了(即使在我的情况下我使用的是POST,而不是GET请求,因此请求的主体包含一些数据)。

I wrote the following simple ASP.NET project to test your issue in IE6. 我编写了以下简单的ASP.NET项目来测试IE6中的问题。 However I'm unable to reproduce (IE6 SP2 running in virtual machine hitting IIS 7.5 shows the alert box from success handler properly). 但是我无法重现(在虚拟机中运行的IE6 SP2命中IIS 7.5正确地显示了成功处理程序的警报框)。 Could you try running it in your environment and reporting whether it works from IE6 for you? 您可以尝试在您的环境中运行它并报告它是否适用于您的IE6?

Note : Sometimes when I cleared IE6 cache and commented out the "SetCacheability" line in ashx.cs, the first click on "Send" button would not show the success alert box, although subsequent clicks did show it. 注意 :有时当我清除IE6缓存并注释掉ashx.cs中的“SetCacheability”行时,第一次单击“发送”按钮将不会显示成功警告框,尽管后续点击确实显示了它。 Maybe all you need is adding "no-cache" headers to the call response in your implementation? 也许您只需要在实现中为调用响应添加“no-cache”标头?

file index.html 文件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>

file Api.ashx 文件Api.ashx

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

file Api.ashx.cs 文件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 + "\"}");
        }
    }
}

What happens if you add a failure function, and alert out the responseText within there? 如果添加失败函数会发生什么,并在那里警告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 http://docs.jquery.com/Ajax/jQuery.ajax#options

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM