Ruby on rails-更新ajax的PUT方法
Ruby on rails - PUT method on update ajax
有人能告诉我为什么这个PUT方法不起作用吗。
$.ajax({
type: "PUT",
dataType: "script",
url: '/resources/35',
data:
{
resource : { pos_y: 45, pos_x: 50 }
}
}).done(function( msg )
{
alert( "Data Saved: " + msg );
});
服务器说我使用了GET方法,但在我的ajax请求中,我有类型:"PUT"
Started GET "/resources/35?resource%5Bpos_y%5D=45&resource%5Bpos_x%5D=50&_=1344001820350" for 192.168.1.89 at 2012-08-03 15:50:20 +0200
Processing by ResourcesController#show as */*
Parameters: {"resource"=>{"pos_y"=>"45", "pos_x"=>"50"}, "_"=>"1344001820350", "id"=>"35"}
User Load (0.3ms) SELECT "users".* FROM "users"
Resource Load (0.1ms) SELECT "resources".* FROM "resources" WHERE "resources"."id" = ? LIMIT 1 [["id", "35"]]
Rendered resources/show.html.erb within layouts/login (2.3ms)
Completed 200 OK in 238ms (Views: 235.9ms | ActiveRecord: 0.4ms)
resources_controller.rb
# PUT /resources/1
# PUT /resources/1.json
def update
@resource = Resource.find(params[:id])
respond_to do |format|
if @resource.update_attributes(params[:resource])
format.html { redirect_to @resource, notice: 'successfully updated.' }
format.js
else
format.html { render action: "edit" }
format.js
end
end
end
我已经尝试添加_method:'put',但它仍然是相同的
$.ajax({
type: "PUT",
dataType: "script",
url: '/resources/35',
data:
{
resource : { pos_y: 45, pos_x: 50 },
_method: 'put'
}
}).done(function( msg )
{
alert( "Data Saved: " + msg );
});
服务器:
"资源%5Pos_y%5D=45&资源%5Pos_x%5D=50&方法=put=1344004390840"
Started GET "/resources/35?resource%5Bpos_y%5D=45&resource%5Bpos_x%5D=50&_method=put&_=1344004390840" for 192.168.1.89 at 2012-08-03 16:33:10 +0200
Processing by ResourcesController#show as */*
Parameters: {"resource"=>{"pos_y"=>"45", "pos_x"=>"50"}, "_"=>"1344004390840", "id"=>"35"}
User Load (0.3ms) SELECT "users".* FROM "users"
Resource Load (0.1ms) SELECT "resources".* FROM "resources" WHERE "resources"."id" = ? LIMIT 1 [["id", "35"]]
Rendered resources/show.html.erb within layouts/login (0.8ms)
Completed 200 OK in 93ms (Views: 90.5ms | ActiveRecord: 0.4ms)
如果有任何帮助,我将不胜感激。
Rails通过值为"put"的参数_method来确定put请求。
由于并非所有浏览器都支持put方法,rails在form_tag中作弊。它将这个输出放入PUT形式:
<input type='hidden' name='_method' value='put'>
所以你要做的是:
$.ajax({
type: "POST",
dataType: "script",
url: '/resources/35',
contentType: 'application/json',
data: JSON.stringify({ resource:{pos_y:45,pos_x:50}, _method:'put' })
}).done(function( msg )
{
alert( "Data Saved: " + msg );
});
相关文章:
- 在put方法之前从作用域获取数据
- dojo/request PUT方法dosn'不起作用
- Ruby on rails-更新ajax的PUT方法
- Spring MVC中的PUT方法HTTP Status 400-AngularJS
- 在react native中通过PUT方法上载文件
- 为什么服务器没有使用 XMLHttpRequest 发送 PUT 方法
- 辛纳屈PUT方法未更新
- 如何在复角中使用PUT方法
- 在Express.js中使用PUT方法
- 我需要覆盖backbone.sync以允许PUT方法
- ExpressJS PUT方法未定义的对象问题
- SoundCloud Javascript SDK 3.0 put方法失败
- AngularJS如何将JSON数据读取到Slim框架(PUT方法)
- Http的Put方法在AngularJS中不起作用
- 使用jQuery调用put方法时,WCF内部服务器错误
- 使用Edm对oData模型进行PUT方法.时间类型
- pre - flight响应中的Access-Control-Allow-Methods不允许使用PUT方法.在AWS
- HTML表单PUT方法
- HTTP服务REST -如何拥有多个PUT方法
- 从Javascript调用PUT方法