Reasons why Browser asks to Save file instead of u

2019-07-18 23:02发布

问题:

Does anybody have a list of reasons why a browser would ask to save a file of JSON data instead of using it to update the page according to functions already present? I had this working for a while, but all of a sudden, I did something that caused that to not work anymore. I am using asp.net MVC4.

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

As you see I have included the appropriate scripts.

@using (Ajax.BeginForm("SearchByDemographic", "SearchPatients", null, new AjaxOptions { HttpMethod = "POST", LoadingElementId = Url.Content("~/Images/ajax-loader.gif"), OnSuccess = "binddata", OnFailure = "FailAlert" }, new { id = "searchByDemographics" }))

I use Ajax.BeginForm() as you can see.

Here is the function that I use to return the JSON result

    [HttpPost]
    public ActionResult SearchByDemographic(SearchByDemographicModel SearchByDemo)
    {
        string UID = HttpContext.User.Identity.Name;
        DataRepository dr = new DataRepository();
        List<SelectListItem> retVal = dr.SearchByDemographic(SearchByDemo, UID);
        if ((retVal == null) || (retVal.Count < 1))
            return Json("Empty Record: No Patient Found", JsonRequestBehavior.AllowGet);
        else
            if(retVal[0].Text.Contains("Error")){
                return Json(new {success = false, nameError = "General Return Exception"}, DataRepository.searchPatientJSonStr(retVal), JsonRequestBehavior.AllowGet);
            }
            else{
                return Json(DataRepository.searchPatientJSonStr(retVal), JsonRequestBehavior.AllowGet);
            }//return PartialView("_RetTable", Json(DataRepository.searchPatientJSonStr(retVal), JsonRequestBehavior.AllowGet));            
    }

This keeps happening to me. I am ignorant of the reasons why this could happen. If I could figure out why this keeps happening I would be in a better position to fix it in the future.

From my knowledge, The browser is expecting JSON data, and somehow or the other, it is not getting it?
Also, Here is JSON of test data being passed back... If it is a little off forgive me, I tried to get a single result out of a list of 20...

"{\"total\":1,\"page\":1,\"records\":1,\"rows\":[{\"id\":11248971,\"cell\":[\"CRAYON \",\" RED \",\" 1956-03-04 \",\" M \",\" 11248971 \",\" 840006723 \",\" 737452545\"]}]}

I still always just get a prompt to open the file.

回答1:

This behaviour is only present in IE due to a configuration option named "Show friendly JSON errrors" which makes ie ask to save your json if it's smaller than a certain size ( i seem to remember 200 characters) instead of consuming it normally

A possible solution is to use

return Json(result, "text/html");

which will make IE behave correctly



回答2:

              public JsonResult Index()
{
         Models.MyEntities Object = new Models.MyEntities();
        var vperson = Object.Person;
        return Json(vperson, "text/html", behavior:JsonRequestBehavior.AllowGet);
   }