views:

333

answers:

4

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?

+1  A: 

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

Luca Matteis
If fiddler shows an HTTP request was issued that means the cache did not come into play.
Milan Gardian
Giving the circumstances, my ajax call doesn't work if I use POST or if I remove `cache: true` and I don't have any control over the server-side coding.
Soufiane Hassou
Could you elaborate why POST and/or caching make your AJAX call not work?
Milan Gardian
about the cache option, if I set it to false, a random number is added to the url which make it "invalid" for the API I'm using. (This is actually an API call)
Soufiane Hassou
I didn't know jQuery adds a unique timestamp to non-cached GET requests (to guarantee they will not be cached :-) ), but confirmed this behaviour in my test project. Could you look at my updated answer and try to run the provided project in your environment to see if the success handler gets called in IE6?
Milan Gardian
A: 

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

James Wiseman
I just tried it, it doesn't show the message.
Soufiane Hassou
Sorry, I misnamed the error function parameter. Should have been error: function(data) - I've edited above to reflect this
James Wiseman
same behavior with error too. :)
Soufiane Hassou
A: 

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 + "\"}");
        }
    }
}
Milan Gardian
+2  A: 

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;
  }
});
Josh Stodola
It seems that xhr is undefined in complete, anyway, I tried with a `alert('complete')`, and it doesn't fire.
Soufiane Hassou