简体   繁体   中英

Set content type based on browser

I am using asp.net mvc 3 and returning json to the user. I'm doing that with:

return Json(results, JsonRequestBehavior.AllowGet)  

results is just a simple viewmodel c# class I created. That works fine in FF and chrome, but then IE 9 asks to open or save the results. Everywhere I look, people say the "fix" is to do something like:

return Json(results, "text/html", JsonRequestBehavior.AllowGet)

This does work for me in IE, but it doesn't work in chrome and/or firefox. It does for some versions, but not all. I was wondering if it's possible to return text/html if the browser is IE, otherwise return the normal JSON. Or is there a better solution? Thanks in advance!

I am assuming that you are doing an AJAX call, if so the solution you have is the correct way to make ALL browsers treat the returned values as HTML, which is a much better solution than dealing with each browser in a different way.

However, in order to be able to work with the returned values, you will need to parse the returned html into JSON.

In the success callback of the AJAX call, simply add the following:

var jsonResponse = $.parseJSON(response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM