【问题标题】:Getting error as Page Not Found(Error Code: 404) when trying $http.get to a URL尝试 $http.get 访问 URL 时出现页面未找到错误(错误代码:404)
【发布时间】:2017-01-20 02:07:28
【问题描述】:

我的 Grails 3.2.3 应用程序在端口 9000 上运行,我正在向 URL /api/mail/getUnreadCount 发出 GET 请求,但我收到该 URL 的 404 错误。

我的URLMapping.groovy 有:

get "/api/$controller(.$format)?"(action: "index")
get "/api/$controller/$id(.$format)?"(action: "show")

"/api/$controller/$action?/$id?(.$format)?" {
    constraints {
        // apply constraints here
    }
}

也帮帮我,我们如何启用 Grails URL 映射日志?

更新 1

class MailController {

    static allowedMethods = [index: "GET", getUnreadCount: "GET"]
    .....
    .......
    def getUnreadCount() {
        User currentUserInstance = springSecurityService.getCurrentUser()
        List counts = userService.getUnreadCounts(currentUserInstance)
        respond(counts)
    }
}

【问题讨论】:

  • 您是否尝试将 '/' 添加到 '/api/mail/getUnreadCount' 使其看起来像 /api/mail/getUnreadCount/
  • 是的,我试过了,但问题仍然存在
  • 我认为问题在于您的 grails 应用程序没有在 9000 端口上正常运行。点击 localhost:9000 检查您是否能够打开 grails 主页。如果它正在工作,那么它是否显示您的 mail 控制器?
  • 是的,localhost:9000 工作正常,即使我正在访问索引操作,它也工作正常。我正在点击localhost:9000/api/mail 之类的 URL,它正在工作,它正在显示来自 mailController 的列表
  • 那么问题是什么?您是说 localhost:9000/api/mail 正在工作,但 localhost:9000/api/mail/getUnreadCount 正在抛出 404?那么一定是打字错误。

标签: grails grails-3.0


【解决方案1】:

预计您在views/mail 目录中有getUnreadCount.gsp 以获取respond 的输出

.. 对于您提供的代码,如果您没有任何视图,您应该得到 Error 500 而不是 404。

在你的代码中,放

格式:['xml', 'json']

或明确提及观点

查看:“showCount”

或尝试render 方法

更新

顺便说一句,请删除

get "/api/$controller/$id(.$format)?"(action: "show")

当您点击 /api/mail/getUnreadCount 时,此映射会导致 $id = getUnreadCount,而您的控制器中没有任何操作 show,这会生成 404 错误。

【讨论】:

    猜你喜欢
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多