How do I PUT data to Rails using JQuery

前端 未结 6 954
半阙折子戏
半阙折子戏 2020-11-30 00:32

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

$.ajax({
          type: \"PUT\",
          url: \'/admin/pages/1.json\',
          data:         


        
6条回答
  •  悲哀的现实
    2020-11-30 01:06

    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.

    Where should I look: JQuery or Rails?

    EDIT:

    I've solved my issue stringifying the json object with a 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 required. In the controller:

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

提交回复
热议问题