IE wants to download JSON result…MVC3

后端 未结 3 1268
既然无缘
既然无缘 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:43

    The proper JsonResult return should look like so:

    [HttpPost] 
    public JsonResult SaveDocument(DocumentModel model, HttpPostedFileBase postedFile)
    {
        ...
        return Json(new { success = true, message="ok" }, "application/json; charset=utf-8", JsonRequestBehavior.AllowGet);
    }
    
    0 讨论(0)
  • 2021-01-22 01:46

    Wild guess: you are using the jquery.form plugin which enables you to upload files using AJAX and you haven't read the documentation which states the following:

    Browsers that support the XMLHttpRequest Level 2 will be able to upload files seamlessly and even get progress updates as the upload proceeds. For older browsers, a fallback technology is used which involves iframes since it is not possible to upload files using the level 1 implmenentation of the XMLHttpRequest object. This is a common fallback technique, but it has inherent limitations. The iframe element is used as the target of the form's submit operation which means that the server response is written to the iframe. This is fine if the response type is HTML or XML, but doesn't work as well if the response type is script or JSON, both of which often contain characters that need to be repesented using entity references when found in HTML markup.

    To account for the challenges of script and JSON responses when using the iframe mode, the Form Plugin allows these responses to be embedded in a textarea element and it is recommended that you do so for these response types when used in conjuction with file uploads and older browsers. Please note, however, that if there is no file input in the form then the request uses normal XHR to submit the form (not an iframe). This puts the burden on your server code to know when to use a textarea and when not to.

    Now that you have read it you should take the respective actions if you want your code to work under IE as I have exemplified in this post.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题