3

So here is my function

function ajax(addr,loading, loadTo, json){
        addr = addr.replace(' ', '');
        if (loading){
                $("#"+loading).fadeIn();
        }

        if (json){
                $.getJSON(addr, function(data){
                        alert('whoooo working'); // <--- it never goes here
                        if (loading){
                                $("#"+loading).fadeOut();
                        }
                        procJSON(data);
                });
                return true;
        }
}

and I'm calling it with

var postid = $(this).attr('data-postid');
ajax(url+'tools/delete/'+postid, 'loading'+postid, false, true);

ajax is sent, image (loading image) is showed, but callback function is never called.

Isn't that just new reserved value from that IE's big list? Yes I know, IE is not a valid browser, but I can't blame my customers

genesis
  • 48,512
  • 18
  • 91
  • 118
  • What Content-Type does the response have? Should be `application/json`, FWIW. *(Also, this is easy to step through with the built-in debugger - does that give any clues?)* – Tomalak Aug 24 '11 at 09:06
  • I've got no idea, but it is good idea to hardcore it – genesis Aug 24 '11 at 09:07
  • @Tomalak: Please answer my question. You're 100% right. Damn, I didn't know IE is so exceptionally – genesis Aug 24 '11 at 09:10
  • @Tomalak: As to your question why didn't I use debugger: I never used IE so I hadn't idea IE has tool for developers – genesis Aug 24 '11 at 09:13
  • IE 8 onwards has debugging tools at F12. Not as sophisticated as Firebug or Chrome, but they do the job. You can make it emulate the behavior (both JS and rendering engine) of older versions of IE there, too. Go ahead and accept @Guffa's answer. If you want to you can give me an up-vote [over here](http://stackoverflow.com/questions/249692/jquery-wont-parse-my-json-from-ajax-query). – Tomalak Aug 24 '11 at 09:27

1 Answers1

6

As it fails in specific browsers, it's likely that it is a combination of unexpected headers in the response, and how the browser handles the data based on that.

If for example the response has the content type text/html instead of application/json, the browser might try to turn the response content into a HTML document (by adding pre tags around it), which would then cause the JSON parsing to fail.

If you use the $.ajax method, you can also catch any error message, which would give you a clue to what's going on:

$.ajax({
  url: addr,
  dataType: 'json',
  success: function(data){
    alert('whoooo working'); // <--- it never goes here
    if (loading){
      $("#"+loading).fadeOut();
    }
    procJSON(data);
  },
  error: function(o,c,m) { alert(m); }
});
Guffa
  • 640,220
  • 96
  • 678
  • 956