如何使用 JQuery 将数据放入 Rails
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/907910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I PUT data to Rails using JQuery
提问by duckyflip
I am trying to send a jquery ajax PUT request that looks like this:
我正在尝试发送一个看起来像这样的 jquery ajax PUT 请求:
$.ajax({
type: "PUT",
url: '/admin/pages/1.json',
data: { page : {...} },
dataType: 'json',
success: function(msg) {
alert( "Data Saved: " + msg );
}
});
but I get the following error:
但我收到以下错误:
The error occurred while evaluating nil.name
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/xml_mini/rexml.rb:29:in `merge_element!'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/xml_mini/rexml.rb:18:in `parse'
(__DELEGATION__):2:in `__send__'
(__DELEGATION__):2:in `parse'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/hash/conversions.rb:154:in `from_xml' ... ...
Is like Rails is trying to parse the params as XML, but I want to use JSON!!
就像 Rails 试图将参数解析为 XML,但我想使用 JSON !
What shall I do to put JSON to rails?
我该怎么做才能将 JSON 放到 rails 中?
回答by duckyflip
PUT and DELETE are not supported by all browsers, RubyOnRails supports passing an extra parameter with your data called _methodwhich will indicate how RoR will treat the request.
并非所有浏览器都支持 PUT 和 DELETE,RubyOnRails 支持在您的数据中传递一个名为_method的额外参数,该参数将指示 RoR 将如何处理请求。
$.ajax({
type: "POST",
url: '/admin/pages/1.json',
data: { _method:'PUT', page : {...} },
dataType: 'json',
success: function(msg) {
alert( "Data Saved: " + msg );
}
});
回答by lqc
The dataType
param in jQuery isn't what you send, but rather specifies the format you expect the answerto be (yes, that's a very poor name). If you want to send your data to the server in a format other then application/x-www-form-urlencoded
you should use contentType
param. You also need to serialize your data
:
dataType
jQuery 中的参数不是您发送的内容,而是指定您期望答案的格式(是的,这是一个非常糟糕的名称)。如果您想以其他格式将数据发送到服务器,application/x-www-form-urlencoded
则应使用contentType
param。您还需要序列化您的data
:
$.ajax({
type: "PUT",
url: '/admin/pages/1.json',
data: JSON.stringify({...}),
contentType: 'application/json', // format of request payload
dataType: 'json', // format of the response
success: function(msg) {
alert( "Data Saved: " + msg );
}
});
回答by lqc
Ok, actually my JSON data didn't have the page key, my mistake. So thats why it was not correctly parsing. But now I get "[object Object]" string as the value for page key instead of a nicely parsed json object.
好吧,实际上我的 JSON 数据没有页面键,我的错误。所以这就是为什么它没有正确解析。但是现在我得到“[object Object]”字符串作为页面键的值,而不是一个很好解析的json对象。
Where should I look: JQuery or Rails?
我应该在哪里看:JQuery 还是 Rails?
EDIT:
编辑:
I've solved my issue stringifying the json object with a script found here: www.json.org/js.html:
我已经解决了使用此处找到的脚本对 json 对象进行字符串化的问题:www.json.org/js.html:
$.ajax({
type: "PUT",
url: '/admin/pages/1.json',
data: { page : JSON.stringify( {...} ) },
dataType: 'json',
success: function(msg) {
alert( "Data Saved: " + msg );
}
});
On the rails side json gem must be required. In the controller:
在 Rails 端必须需要 json gem。在控制器中:
params[:page] = JSON.parse params[:page] if params[:page].is_a? String
回答by dsn raghavendra rao
var id = $('#route_id').val()
$.ajax({
type: 'PUT',
url: '/routes/'+ id,
data: $('#markerform').serializeArray(),
dataType: "JSON",
success: function(data) {
console.log(data);
}
});
回答by theschmitzer
I had a similar problem [object Object] with jQuery/rails, but with HTML, rather than JSON. Perhaps this will help -
我在使用 jQuery/rails 时遇到了类似的问题 [object Object],但使用的是 HTML,而不是 JSON。或许这会有所帮助——
param of [object Object]
参数 [object Object]
data: { lesson: { date: drop_date } }
param of lesson[date]
参数 lesson[date]
data: { "lesson[date]": drop_date }
hope this helps -
希望这可以帮助 -
回答by Carlos A. Cabrera
You probably just need to set the Request Headers on jQuery.
您可能只需要在 jQuery 上设置请求标头。
jQuery.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript");
}
})