【问题标题】:.NET/MVC JSON response opens up dialog box to save.NET/MVC JSON 响应打开对话框以保存
【发布时间】:2010-11-24 09:08:23
【问题描述】:

我试图在我的 .net mvc 应用程序中通过 jquery 的 ajax 方法提交表单。我已将 dataType 设置为 json 并将 contentType 设置为“application/json; charset=utf-8”。在我的控制器操作中,我返回一个 JsonResult。

由于某种原因,JSON 响应没有得到正确处理,而是出现一个对话框来保存其中包含 JSON 对象的文件。

$(document).ready(function() {

    $("#editPageContentForm").submit(function() {

        $.ajax(
  {
      type: "POST",
      dataType: "json",
      url: $("#editPageContentForm").attr("action"),
      contentType: "application/json; charset=utf-8",
      data: { ID: $("#id").val(), small_title: $("#small_title").val(), big_title: $("#big_title").val(), body: $("#body").val(), subheading: $("#subheading").val() }, 

      success: function(result) {
          alert('hi');
      },

      error: function(req, status, error) {
          alert("Sorry! We could not receive your feedback at this time.");
      }
  }
 );
 })

在我的控制器操作中,我有类似的东西:

public JsonResult Edit(int id, string small_title, string big_title, string subheading, string body)
{
     return Json(new {success = true, message = "success"});
}

为什么响应没有以 JSON 格式返回?

【问题讨论】:

    标签: .net jquery asp.net-mvc model-view-controller json


    【解决方案1】:

    您的服务器上的 MIME 类型设置是否正确?

    【讨论】:

      【解决方案2】:

      以 JSON 的形式返回。但是由于您是在表单提交中执行此操作,因此浏览器需要 HTML。浏览器不知道如何以可视方式呈现 JSON,因此它会提示您保存。请注意,此时您的 jQuery 代码不再运行,因为带有表单的页面在浏览器 POST 时已被卸载。

      您的意图并不完全清楚,但如果您想将非 AJAX 提交完全替换为仅 AJAX 提交,请尝试将您的代码更改为:

      $("#editPageContentForm").submit(function(event) {
          event.preventDefault();
          $.ajax(
        {
            type: "POST",
            dataType: "json",
            url: $("#editPageContentForm").attr("action"),
            contentType: "application/json; charset=utf-8",
            data: { ID: $("#id").val(), small_title: $("#small_title").val(), big_title:     
              $("#big_title").val(), body: $("#body").val(), subheading: 
              $("#subheading").val() }, 
            success: function(result) {
                alert('hi');
            },
            error: function(req, status, error) {
                alert("Sorry! We could not receive your feedback at this time.");
            }
        } );
      

      【讨论】:

        【解决方案3】:

        尝试在 $("#editPageContentForm").submit() 函数的末尾抛出 return false;

        【讨论】:

          【解决方案4】:

          我不知道为什么,但是在this topic 中,只需将 JsonResult 的 ContentType 属性设置为“application/json; charset=utf-8”,就解决了类似的问题。

          【讨论】:

            【解决方案5】:

            这似乎是浏览器问题,而不是您的 JsonResult 问题。我在 FireFox 上看到过相同的行为,但在 IE 上没有。

            【讨论】:

              【解决方案6】:

              我遇到了同样的问题并写了blog post about it with more details。无论如何,这是重要的一点:

              Internet Explorer 不喜欢响应中的 Content-Type 标头是“application/json”。将 Content-Type 设置为“text/html” 在返回 JSON 之前专门针对 .asxh 处理程序中的 IE 回应起到了作用。它现在可以在所有浏览器中按预期工作。

              所以尝试更改响应的 ContentType。

              【讨论】:

                【解决方案7】:

                在我的例子中,我从控制器的操作返回一个 ContentResult 而不是 JsonResult。

                public ContentResult FileImportation(HttpPostedFileBase file) {
                 return base.Content(Newtonsoft.Json.JsonConvert.SerializeObject(new {
                  success = true, message = "my message"
                 }));
                }
                

                如您所见,我使用 Newtonsoft.Json 来序列化我的对象,但您可以使用 Json.Convert 内置的 Asp.Net MVC 框架。

                然后,在我的 javascript 代码中,我必须使用服务器的结果调用 $.parseJSON

                onSuccess: function(result) {
                  result = $.parseJSON(result);
                
                  if (result.success) {
                    alert(result.message);
                  } else {
                    alert("...");
                  }
                }
                

                就是这样! 'option' 对象中没有附加参数,例如 'contentType: "text/html"'。

                我还不是解密HTTP 请求的专家,我不知道在选项中指定“contentType”是否会改变某些东西,但对我来说,它有效。

                好的,这就是我在搜索与 Internet Explorer 相关的“XXXX”错误中损失的两个小时。

                【讨论】:

                  猜你喜欢
                  • 2012-12-17
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多