在 Rails 中使用 jQuery 进行 Ajax 请求

发布于 2024-09-03 18:57:51 字数 1050 浏览 8 评论 0原文

我正在使用 jQuery 发送 Ajax 请求。发生的情况是我收到“405 方法不允许”错误。我只是发布一个表单,它将从表单中获取详细信息并将其插入数据库中。只是平常的东西。我使用的是 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

I am sending an Ajax Request using jQuery. What happens is that I am getting an "405 Method Not Allowed" Error. I am just posting a form, which would get the detail from the form and insert it into the DB. Just the usual stuff.I am using WEBrick that comes as default with the rails package. Can somebody please tell me how to fix this.

This is the code that triggers the Ajax Request

$.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2024-09-10 18:57:51

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

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

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

Why are you using a non-restful url to create a comment? Is it possible to use the normal Create action provided by rest? I really recommend and it makes the thinks easier.

If you can do this, this form_for method should do the trick:

<% form_for Comment.new do |f| %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文