IE wants to download JSON result…MVC3

后端 未结 3 1267
既然无缘
既然无缘 2021-01-22 01:01

My MVC3 app uploads documents from the user to our server. I am returning a JsonResult to display any errors, if any:

  [HttpPost] 
    public JsonResult SaveDoc         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-22 01:51

    I ran into a similar problem doing the same in Spring MVC on Java. The problem was that Spring was returning the content-type of the JSON result as application/json, which seems to make IE want to download it. You can try changing the content-type to text/plain; IE won't prompt you to download the file in this case. I suspect that something similar might be happening here.

    You could try:

    return Json(new { success = true, message = "ok" }, "text/plain");
    

    In response to your new problem: the issue is that responseText is just a string. What you need to do is to convert it into a Javascript Object. You can do it like this:

    var response = JSON.parse(responseText);
    if(response.success) {
       ...
    }
    

    Most browsers support JSON.parse(). If you're having issues with non-compliant browsers, you can always use the JSON Javascript Library.

提交回复
热议问题