简体   繁体   中英

How do I PUT JSON data to Ruby on 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 );
      }
});

My controller looks roughly like this:

      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

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

It is like Ruby on Rails is trying to parse the parameters as XML, but I want to use JSON!

What shall I do to put JSON to Ruby on Rails?

Setting the contentType didn't work for me. I was getting a string instead of a hash for params[:page].

So I solved it like this:

Stringifying the JSON object with a script found at JSON in JavaScript :

$.ajax({
    type: "PUT",
    url: '/admin/pages/1.json',
    data: { page : JSON.strigify( {...} ) },
    dataType: 'json',
    success: function(msg) {
        alert( "Data Saved: " + msg );
    }
});

On the Ruby on Rails side, a JSON gem must be required. In the controller:

 params[:page] = JSON.parse params[:page] if params[:page].is_a? String

It is not pretty at all, but it worked for me.

You need to set contentType in your options. contentType is what you are sending. dataType is what you expect back. You should carefully read the documentation on the options argument to ajax .

如果您不想添加脚本,则可以这样做

$.parseJSON("some_jsonish_string")

Also, head's up, but there's a bug in Ruby on Rails 2.3.2 that prevents this from working. See Ruby on Rails 2.3 JSON "put" request routing is broken

You need to set contentType to "application/json" in your options; dataType is only what you expect back.

Also, you need to serialize your data field using JSON.stringify :

$.ajax({
    type: "PUT",
    url: '/admin/pages/1.json',
    data: JSON.stringify({ page :  {...} }),
    contentType: 'application/json',
    dataType: 'json',
    success: function(msg) {
        alert( "Data Saved: " + msg );
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM