Click here to Skip to main content
15,892,965 members
Articles / Web Development / ASP.NET
Tip/Trick

Internet Explorer 9 JsonResult Does Not Return Json in MVC3

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
22 Nov 2015CPOL 12K   4   2
Internet Explorer 8/9 jsonresult method treats response as downloadable json result

Introduction

In Internet Explorer 8 and Internet Explorer 9, the JsonResult ActionResult was returning the content-type of the JSON result as application/json, which seems to make Internet Explorer want to download it. but other browsers were working fine. So we need to override the Jsonresult method in order for it to work in all browsers.

The Code

So here, we use the base controller class that inherits from the controller class, and we set the content type. So you don't need to set content type individually.

JavaScript
public class BaseController : Controller
{
    protected new JsonResult Json(object data)
    {
        if (!Request.AcceptTypes.Contains("application/json"))
            return base.Json(data, "text/plain");
        else
            return base.Json(data);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Software developer in Microsoft technologies ASP.NET, C# ,MVC,KNOCKOUT JS, TELRICK CONTROL, JQUERY, BOOTSTRAP, ORACLE ,SQL SERVER,ENTITY FRAMEWORK,php, oracle

Comments and Discussions

 
Praisegreat Pin
Member 121860504-Dec-15 2:53
Member 121860504-Dec-15 2:53 
SuggestionClarifications and Suggestions Pin
RobTeixeira24-Nov-15 10:46
RobTeixeira24-Nov-15 10:46 
This is a problem that exists going back to IE 7. The problem is not with JsonResult (which actually is setting the proper content type), but the problem is that older versions of IE don't know how to treat JSON responses from a server. Older versions of IE (and I've even seen this happen with Firefox) will attempt to treat any content that isn't browsable in HTML as a file that needs to be downloaded.

You can tell IE that the "application/json" type isn't a file to be downloaded, but this requires messing with IE's registry settings: http://stackoverflow.com/questions/2483771/how-can-i-convince-ie-to-simply-display-application-json-rather-than-offer-to-dow[^]

Also, the "text/plain" type may interfere with client-side libraries that look at the type to determine how to treat or parse the response. It is more correct to use "application/json". Therefore, I would suggest that if you want to manage this problem on the server side by changing the content type, always serve application/json by default, but you can parse the request header for older versions of IE and only change the response content type to text/plain when the request comes from those versions of IE.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.