简体   繁体   中英

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

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.

Any thoughts?

Try using complete instead of 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).

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

I wrote the following simple ASP.NET project to test your issue in 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). Could you try running it in your environment and reporting whether it works from IE6 for you?

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. Maybe all you need is adding "no-cache" headers to the call response in your implementation?

file 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

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

file 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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