最近做项目时,视图中用jquery.form.js异步提交表单时,接收的是JSON格式的数据,但是奇怪的是在IE中提示下载文件,其他浏览器中一切正常,下载后,里面的内容就是在控制器中返回的数据。代码如下:

视图中js代码:

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

控制器中的代码是:

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返回,在前台转换一下,一切正常

05-11 11:07