我正在尝试发送一个类似于以下内容的jQuery Ajax PUT请求:
$.ajax({
type: "PUT",
url: '/admin/pages/1.json',
data: { page : {...} },
dataType: 'json',
success: function(msg) {
alert( "Data Saved: " + msg );
}
});
我的控制器大致长这样:
respond_to do |format|
if @page.update_attributes params[:page]
format.html{ ... }
format.json{ render :json => {:saved => 'ok'}.to_json }
else
format.html{ ... }
format.json{ render :json => {:saved => 'fail'}.to_json }
end
end
但是我收到了以下错误消息。在评估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' ... ...
这就像Ruby on Rails正在尝试将参数解析为XML,但我想使用JSON!
我该怎么做才能在Ruby on Rails中使用JSON?