15

I am using IE8 and I am sending ajax request to on of the url which sends back response as json. The jquery code for the ajax setup is given below:

$(document).ready(function(){
  $.ajax({
    url: url_string,
    dataType: "json",
    success: function(response){
      alert('all is well');
    },
    error: function(request, status, error){
      alert(request);
      alert(status);
      alert(error);
    }
  });
});

I am sure that the server is sending JSON response but IE8 treats it as file and brings up download popup box. But the same process works fine for FF and Chrome. This still happens when i replace json to jsonp in dataType

But it always enters into error callback method.

My json response body consists of a string with html tags too.

Any idea why is this happening?

Thanks

Gagan
  • 4,278
  • 7
  • 46
  • 71
  • can you post the http response headers you're getting back from the server please? Use Fiddler (http://www.fiddler2.com/fiddler2/) to capture. Oh, if that's going to an in-development website on `localhost` - you might have to change to using your machine name or ipv4.fiddler as the hostname once you have it running. – Andras Zoltan Jan 17 '12 at 10:14
  • ok copy pasting response header form FF Response Header Date Tue, 17 Jan 2012 10:21:48 GMT Server Apache/2.2.12 (Ubuntu) X-Powered-By PHP/5.2.10-2ubuntu6.10 Access-Control-Allow-Orig... * Content-Length 6507 Content-Type application/json; charset=utf-8 X-Cache MISS from localhost X-Cache-Lookup MISS from localhost:3128 Via 1.1 localhost:3128 (squid/2.7.STABLE9) Connection keep-alive – Gagan Jan 17 '12 at 11:11
  • and this is the response header that i got from fiddler HTTP/1.0 200 OK Date: Tue, 17 Jan 2012 11:45:41 GMT Server: Apache/2.2.12 (Ubuntu) X-Powered-By: PHP/5.2.10-2ubuntu6.10 Access-Control-Allow-Origin: * Content-Length: 0 Content-Type: application/json; charset=utf-8 X-Cache: MISS from localhost X-Cache-Lookup: MISS from localhost:3128 Via: 1.1 localhost:3128 (squid/2.7.STABLE9) Connection: keep-alive – Gagan Jan 17 '12 at 11:50
  • This isn't specific to jQuery; hence it's a duplicate of [this question](http://stackoverflow.com/questions/13943439/json-response-download-in-ie710). – Kenny Evitt Aug 20 '14 at 20:01

3 Answers3

10

I had the same issue and fixed it by setting Content-type = "text/html" in the response header for all IE requests (rather than "application/json")

I also wrote a blog post about it with some more information: http://blog.degree.no/2012/09/jquery-json-ie8ie9-treats-response-as-downloadable-file/

Andreas
  • 705
  • 10
  • 22
3

Depending on what is sending the json, you have to send it as mime type text. So in rails I had to do this.

    render :text => my_array.to_json

Instead of

    render :json => my_array
Harry Forbess
  • 2,094
  • 4
  • 16
  • 15
0

I modified the url of your code and used the latest version of JQuery and it runs fine within IE8 for me

<html> 
<head>   
  <script src="http://code.jquery.com/jquery-latest.js"></script> 
</head> 
<body> 
<script>  
    $(document).ready(function() {            
    $.ajax({                      
        url: "http://api.flickr.com/services/feeds/groups_pool.gne?id=807213@N20&lang=en-us&format=json&jsoncallback=?",
                    dataType: "json",                      
        success: function(response){                        
            alert('all is well');                      
            alert($.param(response));                 
        },                      
        error: function(request, status, error){                            
            alert(request);                            
            alert(status);                            
            alert(error);
        }          
});  
}); 
</script> 
</body>
</html>

There is a known issue as detailed in this answer where IE8 has problems with an extra comma in a result array. Check the contents of the response alert for this.

Community
  • 1
  • 1
Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
  • 1
    Thanks for your answer, but this is not solving my problem. My problem is, why IE8 downloaded the json response as a file. Content-Length of response header is 0 as the response is treated as a file. – Gagan Jan 17 '12 at 12:07
  • Yes, I seen that in your comment added after I answered. – Nicholas Murray Jan 17 '12 at 13:28