Ruby on rails - PUT method on update ajax

前端 未结 1 1549
夕颜
夕颜 2020-12-16 19:34

Could someone tell me why this PUT method doesn\'t work please.

$.ajax({
        type: \"PUT\",
        dataType: \"script\",
        url: \'/resources/35\',         


        
1条回答
  •  無奈伤痛
    2020-12-16 20:12

    Rails determines the put request via the parameter _method with the value 'put'.

    As not all the browsers support the put method, rails cheats in the form_tag. its putting this output in a PUT form:

    
    

    So what you have to do is this:

    $.ajax({
        type: "POST",
        dataType: "script",
        url: '/resources/35',
        contentType: 'application/json',
        data: JSON.stringify({ resource:{pos_y:45,pos_x:50}, _method:'put' })
    }).done(function( msg )
            {
                alert( "Data Saved: " + msg );
            });
    

    0 讨论(0)
提交回复
热议问题