【问题标题】:Rails ajax call 404 not found routing errorRails ajax call 404 not found 路由错误
【发布时间】:2014-08-11 18:31:21
【问题描述】:

我正在使用 ruby​​ on rails 并尝试进行 ajax 调用,但一直看到错误。 所以在我的 index.html.erb 中,我有一个带有 id 测试的按钮。在同一个索引文件中,我有

("#test").click(function(){
    $.ajax({ 
      type: 'POST', 
      url: '/test', 
      data: {key: 'value'}, 
      dataType: 'script',
    });
});

所以,当我点击带有 id test 的按钮时,我一直看到

POST http://localhost:3000/test 404 (Not Found)

我似乎无法弄清楚为什么,因为在我的 config/routes.rb 中,我得到了 '/test',到:'prompts#test' 并且在我的 promptsController 中,我有

def test
  puts "enters here"
end

【问题讨论】:

  • 添加有问题的rake routes的输出

标签: jquery ruby-on-rails ruby ajax


【解决方案1】:

在你的路线中你说你有

get '/test', to: 'prompts#test' 

但请注意,在您对 $.ajax() 的调用中,您使用的是 POST

相反,您需要一条路线

post '/test', to: 'prompts#test' 

【讨论】:

    猜你喜欢
    • 2019-04-06
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2015-01-07
    • 1970-01-01
    相关资源
    最近更新 更多