How do I PUT data to Rails using JQuery

前端 未结 6 955
半阙折子戏
半阙折子戏 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 00:48

    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). 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. You 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 );
          }
    });
    

提交回复
热议问题