gpt4 book ai didi

javascript - Rails 4 使用 xml 和 jquery 更新数据

转载 作者:行者123 更新时间:2023-11-28 08:33:48 24 4
gpt4 key购买 nike

我正在尝试使用 jquery 中的 ajax 调用自动更新数据。

当对象移动时,将执行以下代码:

这是我的 jquery 代码

$.ajax({
type: "PUT",
url: "/devices/2328.xml",
data: '<device><name>test</name></device>',

contentType: 'application/xml', // format of request payload
dataType: 'html', // format of the response
});

我的 Controller 如下所示:

def update
@device = Device.find(params[:id])
if @device.update_attributes(device_params)
respond_with @device
else
render :action => 'edit'
end
end

private
def device_params
params.require(:device).permit(:name)
end

在我的日志中,我可以看到请求传入,但实际上什么也没发生,请参阅底部的 Completed 204 No Content我还注意到我传递的值在日志的参数部分找不到

Started PUT "/devices/2328.xml" for 127.0.0.1 at 2014-02-01 19:10:33 +0100
Processing by DevicesController#update as XML
Parameters: {"id"=>"2328"}

Device Load (0.4ms) SELECT `devices`.* FROM `devices` WHERE `devices`.`id` = 2328 LIMIT 1

(0.1ms)开始 (0.1ms) 提交 9 毫秒内完成 204 无内容(ActiveRecord:0.6 毫秒)

我真的不知道出了什么问题。

有人知道吗?

最佳答案

我没有看到 data: 'test' 参数被传递。这可能是一个键/值对问题。

更改 data: { 'device': { 'name': 'test' }}

$.ajax({ type: "PUT", url: "/devices/2328.xml", 
data: { 'device': { 'name': 'test' }},
contentType: 'application/xml', // format of request payload
dataType: 'html', // format of the response
});

关于javascript - Rails 4 使用 xml 和 jquery 更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21502424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com