How to set up data for Rails using jQuery

I am trying to send a jQuery ajax PUT request that looks like this:

$.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' ... ... 

How Rails tries to parse parameters as XML, but I want to use JSON !!

What should I do to put JSON on the rails?

+48
json jquery ajax ruby-on-rails
May 25 '09 at 20:10
source share
6 answers

PUT and DELETE are not supported by all browsers, RubyOnRails supports passing an additional parameter with your _method data, which will indicate how RoR will handle the request.

 $.ajax({ type: "POST", url: '/admin/pages/1.json', data: { _method:'PUT', page : {...} }, dataType: 'json', success: function(msg) { alert( "Data Saved: " + msg ); } }); 
+84
May 25 '09 at 20:31
source share

The dataType parameter in jQuery is not what you are sending, but rather determines the format you expect from the response (yes, this is a very bad name). If you want to send your data to the server in a format other than application/x-www-form-urlencoded , you must use the contentType parameter. You will also need to serialize your 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 ); } }); 
+35
Oct 27 '10 at 17:50
source share

Well, actually my JSON data did not have a page key, my error. That is why he did not understand correctly. But now I get the string "[Object Object]" as the value for the page keyword, and not a well-parsed json object.

Where should I look: jQuery or Rails?

EDIT:

I decided to solve the problem using the json object using the script found here: 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 needed. In the controller:

  params[:page] = JSON.parse params[:page] if params[:page].is_a? String 
+5
May 25 '09 at 21:23
source share

I had a similar [object Object] problem with jQuery / rails, but with HTML, not JSON. Maybe this will help -

param [object Object]

 data: { lesson: { date: drop_date } } 

param lesson[date]

 data: { "lesson[date]": drop_date } 

hope this helps -

+1
Dec 07 '09 at 13:00
source share
 var id = $('#route_id').val() $.ajax({ type: 'PUT', url: '/routes/'+ id, data: $('#markerform').serializeArray(), dataType: "JSON", success: function(data) { console.log(data); } }); 
+1
Oct 27 '16 at 11:50
source share

You probably just need to set the request headers in jQuery.

 jQuery.ajaxSetup({ 'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript"); } }) 
0
Dec 22 '10 at 2:30
source share



All Articles