【问题标题】:Ajax Request using jQuery in Rails在 Rails 中使用 jQuery 的 Ajax 请求
【发布时间】:2010-06-02 20:23:43
【问题描述】:

我正在使用 jQuery 发送 Ajax 请求。发生的情况是我收到“405 Method Not Allowed”错误。我只是发布一个表格,它将从表格​​中获取详细信息并将其插入数据库。只是通常的东西。我使用的是rails包默认提供的WEBrick。谁能告诉我如何解决这个问题。

这是触发 Ajax 请求的代码

$.post($(this).attr("action") + ".js",$(this).serialize(),null,"script");

Response Headers
Cache-Control   no-cache
Allow   GET, PUT, DELETE
Content-Type    text/html; charset=utf-8
Content-Length  9502
Server  WEBrick/1.3.1 (Ruby/1.9.1/2009-12-07)
Date    Wed, 02 Jun 2010 20:41:33 GMT
Connection  Keep-Alive

Request Headers
Host    localhost:3000
User-Agent  Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept  application/json, text/javascript, */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With    XMLHttpRequest
Referer http://localhost:3000/viewspot/3
Content-Length  141
Pragma  no-cache
Cache-Control   no-cache

【问题讨论】:

  • 您似乎调用了错误的 url。您可以粘贴您的 form_for 调用吗?
  • {:action => "add_comment"},:html=>{:id=>"add_comment",:method=>:post}做 |f|%>

标签: jquery ruby-on-rails ajax webrick


【解决方案1】:

【讨论】:

    【解决方案2】:

    为什么要使用非静态 URL 来创建评论?是否可以使用休息提供的正常创建动作?我真的推荐它,它使思考更容易。

    如果你能做到这一点,这个 form_for 方法应该可以解决问题:

    <% form_for Comment.new do |f| %>
    

    【讨论】:

      猜你喜欢
      • 2011-02-27
      • 2014-07-28
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 2018-03-20
      相关资源
      最近更新 更多