[英]How do I PUT data to Rails using JQuery
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试图将params解析为XML,但我想使用JSON !!
What shall I do to put JSON to rails? 如何将JSON放入rails?
PUT and DELETE are not supported by all browsers, RubyOnRails supports passing an extra parameter with your data called _method which 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 );
}
});
The dataType
param in jQuery isn't what you send, but rather specifies the format you expect the answer to be (yes, that's a very poor name). jQuery中的dataType
参数不是你发送的,而是指定你期望答案的格式(是的,这是一个非常糟糕的名字)。 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. 如果要以application/x-www-form-urlencoded
以外的格式将数据发送到服务器,则应使用contentType
参数。 You also need to serialize your data
: 您还需要序列化您的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 );
}
});
Ok, actually my JSON data didn't have the page key, my mistake. 好吧,实际上我的JSON数据没有页面键,我的错误。 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. 但现在我得到“[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. 在轨道上,必须要求json gem。 In the controller: 在控制器中:
params[:page] = JSON.parse params[:page] if params[:page].is_a? String
I had a similar problem [object Object] with jQuery/rails, but with HTML, rather than JSON. 我有一个类似的问题[对象]与jQuery / rails,但使用HTML,而不是JSON。 Perhaps this will help - 也许这会有所帮助 -
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 - 希望这可以帮助 -
var id = $('#route_id').val()
$.ajax({
type: 'PUT',
url: '/routes/'+ id,
data: $('#markerform').serializeArray(),
dataType: "JSON",
success: function(data) {
console.log(data);
}
});
You probably just need to set the Request Headers on jQuery. 您可能只需要在jQuery上设置Request Headers。
jQuery.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript");
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.