65.9K
CodeProject is changing. Read more.
Home

Internet Explorer 9 JsonResult Does Not Return Json in MVC3

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Nov 23, 2015

CPOL
viewsIcon

12287

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.

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);
    }
}