use*_*922 10 javascript ruby jquery controller ruby-on-rails
有人可以告诉我为什么这个PUT方法不起作用.
$.ajax({
type: "PUT",
dataType: "script",
url: '/resources/35',
data:
{
resource : { pos_y: 45, pos_x: 50 }
}
}).done(function( msg )
{
alert( "Data Saved: " + msg );
});
Run Code Online (Sandbox Code Playgroud)
服务器说我已经使用了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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我试过添加_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 );
});
Run Code Online (Sandbox Code Playgroud)
服务器:
"资源%5Bpos_y%5D = 45&资源%5Bpos_x%5D = 50&method = 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)
Run Code Online (Sandbox Code Playgroud)
我很感激任何帮助.
Mic*_*per 27
Rails通过参数_method确定put请求,值为'put'.
由于并非所有浏览器都支持put方法,因此在form_tag中使用rails作弊.它将此输出置于PUT形式:
<input type='hidden' name='_method' value='put'>
Run Code Online (Sandbox Code Playgroud)
所以你需要做的是:
$.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 );
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13407 次 |
| 最近记录: |