【问题标题】:why did I get HTTP Status 404, when I use withformat to render results as json in grails?当我在 grails 中使用 withformat 将结果呈现为 json 时,为什么会得到 HTTP 状态 404?
【发布时间】:2013-07-07 16:45:17
【问题描述】:

我正在编写一个 grails 应用程序。我使用 withFormat 来呈现结果。代码如下:

class TestController{
   def index(){
      def result = ["a":"1"]
      withFormat {
         json{ render result as JSON }
         xml{ render result as XML}
      }
   }
}

我使用网址 .../test.../test/index.json.../test/index 来测试一下。它给了我一个HTTP Status 404 错误并告诉我the requested resource is not available. 我不确定为什么会这样。

【问题讨论】:

  • 我猜我会说 URL 没有指向文件。三个领先的. 可能是原因。试试../test。 (只有 2 个.
  • 您使用哪个版本的 Grails?在 2.2.2 中使用上述(正是您的)控制器实现时,它按预期对我有用。

标签: json url grails http-status-code-404 render


【解决方案1】:

可能发生的情况是,在您的 withFormat 块的任何闭包中都没有处理请求。由于您没有后备渲染,因此您将获得 404,因为可能没有 index.gsp。您应该调试该操作并了解如何处理请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2023-03-10
    • 2011-05-03
    • 2016-05-02
    • 2021-11-18
    相关资源
    最近更新 更多