zoukankan      html  css  js  c++  java
  • asp.net MVC控制器中返回JSON格式的数据时提示下载

     Asp.net mvc在接收的是JSON格式的数据,但是奇怪的是在IE中提示下载文件,其他浏览器中一切正常,下载后,里面的内容就是在控制器中返回的数据。代码如下:

    视图中js代码:

       $("#form").ajaxSubmit({
                        type: "POST",
                        url: "/controller/action/",
                        datatype: "json",
                        success: function (data) {
                          alert(data.Msg);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                        }
                    });

    控制器中的代码是:

    ResultJsonInfo 为自定义类

     public JsonResult DoUploadModel(Model3DInfo model)
            { 

          //其他代码省略
                    return Json(new ResultJsonInfo() { Result = true, Msg = "保存成功!" });

            }

    解决方法如下,只需要修改两点,修改过的代码如下:

    控制器中:

    public JsonResult DoUploadModel(Model3DInfo model)
            { 

         ...
                return Json(new ResultJsonInfo() { Result = true, Msg = "保存成功!" }, "text/html");
            }

    视图中:

       $("#formDoUpload").ajaxSubmit({
                        type: "POST",
                        url: "/controller/action/",
                        datatype: "json",
                        success: function (data) {
                               data = JSON.parse(data); 

              alert(data.Msg);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                        }
                    });

    红色的部分就是我修改的地方,让其返回按照text/html返回,在前台转换一下,一切正常

  • 相关阅读:
    Educational Codeforces Round 22 C. The Tag Game
    Codeforces Round #421 (Div. 1) B. Mister B and PR Shifts(技巧)
    Codeforces Round #422 (Div. 2) D. My pretty girl Noora
    Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!
    hdu3756(三分)
    hihocoder1496(高维前缀和)
    AOJ731(不等式)
    UVALive7042(博弈论)
    Codeforces 284E(概率)
    hdu4778(状态压缩dp)
  • 原文地址:https://www.cnblogs.com/chenghm2003/p/4775049.html
Copyright © 2011-2022 走看看