WARNING: Can't verify CSRF token authenticity rails

后端 未结 17 1034
生来不讨喜
生来不讨喜 2020-11-22 06:05

I am sending data from view to controller with AJAXand I got this error:

WARNING: Can\'t verify CSRF token authenticity

I think

17条回答
  •  鱼传尺愫
    2020-11-22 06:32

    Indeed simplest way. Don't bother with changing the headers.

    Make sure you have:

    <%= csrf_meta_tag %> in your layouts/application.html.erb
    

    Just do a hidden input field like so:

    
    

    Or if you want a jQuery ajax post:

    $.ajax({     
        type: 'POST',
        url: "<%= someregistration_path %>",
        data: { "firstname": "text_data_1", "last_name": "text_data2", "authenticity_token": "<%= form_authenticity_token %>" },                                                                                  
        error: function( xhr ){ 
          alert("ERROR ON SUBMIT");
        },
        success: function( data ){ 
          //data response can contain what we want here...
          console.log("SUCCESS, data="+data);
        }
    });
    

提交回复
热议问题