Как получить правильные параметры через ajax - PullRequest
0 голосов
/ 06 августа 2020

Я хочу передать полный объект данных контроллеру через вызов ajax, но параметры не получают надлежащего формата. ниже мой код ajax.

function fileUpdate(){
var values = []
skill = $("#skill").val();
var cnf = confirm("Are you sure?");
if ( cnf == true) {
$.ajax({
      type: 'post',
      url: '/user_skill_update',
      dataType: 'json',
      data:  { skill: skill},
      success: function (msg) {
      },
      error: function (err){
        console.log(err)       
      }   
    });
    $('#reset')[0].click();
    alert(skill);
}

}

   # use object to pass through ajax call
  <%= hidden_field_tag :skill, value: @data %> 
  <%= button_to "update", 'javascript:void(0)', id:'updateFileRank', class: 'btn btn-info btn-xl', style:"margin-top: 80px;margin-left:-26px;", onclick: 'fileUpdate();' %>


   # ouput
   {"skill"=>"{:value=>[{\"dept_id\"=>\"01\", \"user_id\"=>\"001\", \"level_cd\"=>\"04_swim\", \"first_name\"=>\"rohit\", \"last_name\"=>\"patel\", \"dept_full_name\"=>\"swiming\", \"rank\"=>\"04_swim\"}, {\"dept_id\"=>\"02\", \"user_id\"=>\"002\", \"level_cd\"=>\"04_swim\", \"first_name\"=>\"ranjit\", \"last_name\"=>\"shinde\", \"dept_full_name\"=>\"running\", \"rank\"=>\"03_run\"}, {\"dept_id\"=>\"04\", \"user_id\"=>\"004\", \"level_cd\"=>\"02_jump\", \"first_name\"=>\"kedar\", \"last_name\"=>\"patil\", \"dept_full_name\"=>\"jumping\", \"rank\"=>\"02_jump\"}, {\"dept_id\"=>\"05\", \"user_id\"=>\"005\", \"level_cd\"=>\"03_run\", \"first_name\"=>\"kapil\", \"last_name\"=>\"bote\", \"dept_full_name\"=>\"Hammer\", \"rank\"=>\"03_run\"}



   # I need this format output.
  {"skill"=>"{:value=>[{"dept_id"=>"01", "user_id"=>"001", "level_cd"=>"04_swim", "first_name"=>"rohit", "last_name"=>"patel", "dept_full_name"=>"swiming", "rank"=>"04_swim"}, {"dept_id"=>"02", "user_id"=>"002", "level_cd"=>"04_swim", "first_name"=>"ranjit", "last_name"=>"shinde", "dept_full_name"=>"running", "rank"=>"03_run"}, {"dept_id"=>"04", "user_id"=>"004", "level_cd"=>"02_jump", "first_name"=>"kedar", "last_name"=>"patil", "dept_full_name"=>"jumping", "rank"=>"02_jump"}, {"dept_id"=>"05", "user_id"=>"005", "level_cd"=>"03_run", "first_name"=>"kapil", "last_name"=>"bote", "dept_full_name"=>"Hammer", "rank"=>"03_run"}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...