Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE8 treats json response as file and tries to download it

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

like image 805
Gagan Avatar asked Jan 17 '12 10:01

Gagan


2 Answers

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/

like image 90
Andreas Avatar answered Nov 08 '22 15:11

Andreas


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
like image 3
Harry Forbess Avatar answered Nov 08 '22 15:11

Harry Forbess