浏览器要求保存文件而不是使用JSON结果更新页面的原因 [英] Reasons why Browser asks to Save file instead of updating page with JSON result

查看:152
本文介绍了浏览器要求保存文件而不是使用JSON结果更新页面的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能列出为什么浏览器会要求保存JSON数据文件而不是根据现有功能使用它来更新页面的原因吗?我做了一段时间,但是突然之间,我做了一些使它不再起作用的事情.我正在使用asp.net MVC4.

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

如您所见,我使用Ajax.BeginForm().

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

这是我用来返回JSON结果的函数

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.

据我所知,浏览器期望使用JSON数据,或者某种程度上,它没有得到它?
另外,这是测试数据的JSON被传回...如果有点不原谅,我试图从20个列表中获取单个结果...

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.

推荐答案

由于配置选项"Show friendly JSON errrors"(显示友好的JSON错误),IE仅在IE中存在此行为,即要求保存您的json(如果它小于某个特定值)大小(我似乎记得200个字符)而不是正常使用

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

可能的解决方案是使用

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

这将使IE正常运行

这篇关于浏览器要求保存文件而不是使用JSON结果更新页面的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆