【问题标题】:No route matches [GET] error when sending a post request with ajax使用 ajax 发送 post 请求时没有路由匹配 [GET] 错误
【发布时间】:2016-09-25 23:13:33
【问题描述】:

通过 ajax 发送一个 post 请求,但收到上面的错误,“没有路由匹配 [GET]”。

ajax 是这样的

$.ajax({
   type: 'POST',
   url: '/stage_progress',
   data: {'subject_id' : subject, 'course_id' : course, "chapter_id" : chapter, "stage_id" : stage, "user_id" : user, "session" : "0", "completed" : "true"},
   success: function(){
       console.log('got it');
   }
});

routes.rb

post '/stage_progress', to: 'stage_progress#track_progress'

如果我运行 rake 路由,它表明存在到此 url 的发布路由,但由于某种原因,ajax 正在创建一个“get”请求。任何解释为什么以及如何解决它的帮助将不胜感激。

【问题讨论】:

  • 您能否将您的url 属性向上移动,请同时检查您的路线..
  • 或者你可以as阻止你的路线,以确保你有像这样的正确路线post '/stage_progress', to: 'stage_progress#track_progress', as:start_progress
  • 如果您打开浏览器的网络选项卡,ajax 请求看起来是否正确?它是否点击了正确的网址,它实际上是一个 POST 吗?这将告诉您问题是在客户端(如果它以某种方式发送 GET)还是在服务器端(在这种情况下可能是路由问题)

标签: ajax ruby-on-rails-4


【解决方案1】:

查看代码时,您将覆盖控制器“stage_progress”的默认索引操作。调用“/stage_progress”只会路由到默认索引操作(get)。我猜您要么将路线“/stage_progess”重命名为其他名称,这将解决问题。

或者在其他情况下您仍然想要相同的路线,然后尝试将其写在所有其他路线之上,

post '/stage_progress', to: 'stage_progress#track_progress'
resources 'stage_progress'

因为路由总是在 routes.rb 文件中从上到下映射。如果在 'POST' 路由声明之上还有任何其他 'GET' 路由,它仍然只会采用配置的第一个路由。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    相关资源
    最近更新 更多