19

I've faced strange issue with IE (11). I have simple jQuery code:

$("#my-radio").click(function () {
     $.ajax({
        url: "/my-url",
    }).error(function(jqXHR, textStatus, errorThrown) {
        $("#error-summary").text(errorThrown);
    });
});

The thing is that this code works perfectly in all browsers except IE (in my case except IE 11, didn't test in earlier versions yet) BUT, when I open IE built in javascript debugger to see what's wrong - everything starts to works just fine. IE makes ajax call to controller action, breakpoint in controller gets hit. Action returns nothing (it's of type void), just counting some stuff. I've already tried plugin solution provided in this thread but it wasn't useful.

UPDATES:

To demonstrate the issue I've published simple MVC application here. When you click on image, browser makes ajax request to this action:

public string Hello()
{
     string ip = Request.ServerVariables["REMOTE_ADDR"];
     string agent = Request.UserAgent;    

     var request = new Request
     {
          IP = ip,
          Time = DateTime.UtcNow,
          UserAgent = agent
     };

     _requestsRepository.Add(request);

     int byIp = _requestsRepository.GetTotalRequestsCountByUser(ip);
     int total = _requestsRepository.TotalRequests;

     string message = string.Format("Hello, {0}. This is your {1}th request and {2}th request in total.", ip, byIp, total);

     return message;
 }

with this jQuery code:

$(document).ready(function () {
    $("#click-me").click(function () {
        $.ajax({
            url: "/services/hello",
        })

        .success(function (data) {
            alert(data);
        })

        .error(function (jqXHR, textStatus, errorThrown) {
            $("#error-summary").text(errorThrown);
        });
    });
});

As you can see - action returns simple string which is alerted on success. You can try this page in IE. You will notice that the amount of requests doesn't grow, because NO request is actually sent but still success event handler is called and message is alerted (this is probably some old cached message after some possibly one successful request, I'm not sure). In any other browser the amount of requests will grow, because they are sent each time you click the image. If you turn on IE debugger - requests will be sent too. I suggest this is some kind of IE cache related issue.

Community
  • 1
  • 1
Dmytro
  • 16,668
  • 27
  • 80
  • 130
  • Is this the only code or do you have console.log somewhere? – artm Nov 17 '14 at 12:50
  • @artm, it's the only almost exact code. No `console.log()`s. – Dmytro Nov 17 '14 at 12:52
  • 3
    I experienced this before (IE9 I think it was), page was coming up empty in IE, when I opened the console debugger it was working fine, turned out `console.log` was throwing undefined exceptions because console wasn't open. When you open the console it works. Might be another issue but maybe related. – artm Nov 17 '14 at 12:55
  • so what is the question **works in IE or works in all browsers except IE**? – Jai Nov 17 '14 at 12:55
  • If you use Fiddler it shows network activity when F12 is not opened? – Vitor Canova Nov 17 '14 at 14:36
  • @artm, how did you knew that console was throwing undefined exception without opening the console? – Dmytro Nov 17 '14 at 18:27
  • @Jai, it works in IE **ONLY IF DEBUGGER IS ON (F12)** – Dmytro Nov 17 '14 at 18:28
  • 1
    @VitorCanova, I haven't try fidler. Will take a look... – Dmytro Nov 17 '14 at 18:30
  • @DmytroTsiniavskyi Can you upload a demo? – Sampson Nov 17 '14 at 20:10
  • Try `window.onerror = function(msg, url, line, col, error) { alert(error);}` and see if you get anything. – artm Nov 17 '14 at 22:25
  • @JonathanSampson, see my updates... – Dmytro Nov 17 '14 at 23:01
  • @artm, already tried. There are no errors. IE thinks that request was successful. – Dmytro Nov 17 '14 at 23:02
  • @DmytroTsiniavskyi Have you tried setting jQuery's `cache` setting to false? – Sampson Nov 17 '14 at 23:04
  • 1
    You are definitely getting a cached version of the request. It might be worth trying setting the no-cache headers. Similar to this bug http://stackoverflow.com/questions/9234044/asp-net-mvc-and-ie-caching-manipulating-response-headers-ineffective – Anthony Roberts Nov 17 '14 at 23:39
  • @JonathanSampson, yes, this actually helped, thanks... If you transform your comment to an answer I will mark it as accepted one. – Dmytro Nov 17 '14 at 23:39
  • @AnthonyRoberts, yes, thanks, I've already figured this stuff out... I should forbid cache if browser is ie... – Dmytro Nov 17 '14 at 23:48
  • @DmytroTsiniavskyi What are your request/response headers? Internet Explorer should only cache if the headers suggest it is safe to do so. jQuery's `cache` option is good, but it's not as good as just fixing the headers to begin with. – Sampson Nov 18 '14 at 00:02

1 Answers1

29

It sounds like you may be getting cached responses. The developer tools may have the Always Refresh from Server option enabled, causing you to only get a fresh response when the tools themselves are opened.

Try adding the cache option in the jQuery AJAX options object, and set its value to false.

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • Thank you! I had the same problem, which seemed really bizarre until this explanation. – Michael Winther Sep 10 '15 at 13:22
  • 2
    works for me too. Another reason is console.log. Remove console.log from your code. – arpan desai Jul 18 '16 at 07:53
  • isn't that bizzare? – Kishor Pawar Nov 17 '16 at 10:48
  • this is absolute bizzare, caused me lots of time , every time works in chrome and not works in IE , and in IE debug mode it was working , explanation just solved my issue . – Asraful Sep 21 '17 at 15:34
  • Are you KIDDING ME!! I have been trying to solve this for NINE HOURS!! Dang client insists on using IE. Could NOT figure out why it worked in Chrome but not IE. FINALLY stumbled upon the fact that it worked when debug was open, but not otherwise. Once short google query later, and I found this post. THANK YOU!! – Casey Crookston Dec 10 '17 at 06:02