【问题标题】:jsonresult is returning html not jsonjsonresult 返回的是 html 而不是 json
【发布时间】:2016-08-03 20:01:58
【问题描述】:

我向 Godaddy 发布了一个 ASP.NET MVC 应用程序。我遇到了一个 Ajax 调用问题,它应该返回一个 JSON 对象,它返回我网站索引页的 HTML。我以前在应用程序的菜单栏链接上遇到问题,它们会重定向到我网站的主页。我能够通过向我的网站的 web.config 添加一条规则来解决问题,该规则排除了包含该应用程序的子文件夹:<add input="{REQUEST_URI}" pattern="^/(codesnippetapp)" negate="true" />我检查了 Chrome 中的开发控制台,请求 URL 是错误的。 URL 应该是 http://www.mattdailey.net/codesnippetapp/Home/GetCodeData 而不是 http://www.mattdailey.net/Home/GetCodeData

这里是 Ajax 调用和检索 JSON 的 JsonResult 函数:

$.ajax({
            url: '/Home/GetCodeData',
            type: 'Post',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: JSON.stringify(selectedSnippetID),
            success: function (data) {
                if (data.success) {
                    $("#snippetcode").val(data.snippetCode);                    
                } else {
                    alert('invalid ID' + data.success);
                }
            }
        });
    [HttpPost]
    public JsonResult GetCodeData(int snippetID)
    {

        CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID);
        if (returnedsnippet != null)
        {
            return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode });
        }
        return Json(new { success = false });

    }

我需要在我的应用的 web.config 中添加什么?还是我需要在我的网站的 web.config 中添加代码?

更新: 我尝试使用 GET 方法,但出现内部服务器错误。我使用 razor @section 代码将脚本从外部文件移动到视图本身,如下所示:

@section Scripts 
{
... jQuery code
}

然后将此添加到_Layout.cshtml:

@RenderSection("Scripts", required: false)

我确实将 razor @Url.Action 助手添加到 Ajax url。我还改变了将应用程序发布到 Godaddy 的方式,我认为这也有帮助。我从 FTP 方法更改为文件系统。然后我通过 FTP 手动上传文件。它现在可以工作了。

感谢大家的帮助。我写下了我的步骤,希望这能帮助遇到类似情况的其他人。

【问题讨论】:

    标签: asp.net json asp.net-mvc web-config


    【解决方案1】:

    / 在 url 值的开头将使其成为您网站的根目录(不是您的应用程序)。

    使用Url.Action 辅助方法生成您的操作方法的路径。

    url: '@Url.Action("GetCodeData","Home")',
    

    如果您的 javascript 在 razor 视图中,这应该可以工作。如果您的代码在外部 js 文件中,请在 razor 视图中调用此方法并将其分配给一个变量并在您的 js 文件中使用它,如this post第二部分所述

    【讨论】:

      【解决方案2】:

      使用 JsonRequestBehavior.AllowGet

      public JsonResult GetCodeData(int snippetID)
      {
      
          CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID);
          if (returnedsnippet != null)
          {
              return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode },JsonRequestBehavior.AllowGet);
          }
          return Json(new { success = false },JsonRequestBehavior.AllowGet);
      
      }
      

      【讨论】:

      • 您不需要为 HttpPost 操作/调用(OP 正在执行的操作)指定 JsonRequestBehaviour.AllowGet。仅当您从 GET 操作方法返回一些 json 时才需要它。
      【解决方案3】:

      当您尝试将 Json 返回给客户端时,这应该是一个 GET 操作。

      $.ajax({
                  url: '/Home/GetCodeData',
                  type: 'GET',
                  contentType: 'application/json; charset=utf-8',
                  dataType: 'json',
                  data: JSON.stringify(selectedSnippetID),
                  success: function (data) {
                      if (data.success) {
                          $("#snippetcode").val(data.snippetCode);                    
                      } else {
                          alert('invalid ID' + data.success);
                      }
                  }
              });
      
          public JsonResult GetCodeData(int snippetID)
          {
      
              CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID);
              if (returnedsnippet != null)
              {
                  return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode }, JsonRequestBehavior.AllowGet);
              }
              return Json(new { success = false }, JsonRequestBehavior.AllowGet);
      
          }
      

      【讨论】:

        猜你喜欢
        • 2021-01-30
        • 1970-01-01
        • 2022-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-27
        相关资源
        最近更新 更多