1

I have an array in my show.html.erb page and want to pass it from javascript to a method in the Controller using an Ajax request , But it gives me error HTTP/1.1 422 Unprocessable Entity and i have no clue where to search

here is my JS code:

function create_ajax_request (url, data, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open('PUT', url, true);
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.setRequestHeader('Accept', 'application/json'); // I want JSON 
  xhr.responseType = "json";
  xhr.addEventListener('load', function() {
    xhr.responseJSON =  xhr.response;
    console.log(xhr.responseJSON);
    callback(xhr.responseJSON,  xhr);
   });
  xhr.send( JSON.stringify(data) );
  return xhr;
}

function submit()
{
  var SelectedValue = [];
  var name = 0;
  var radios = document.getElementsByTagName("input");
  for(var i=0; i<radios.length; i++) {
    if(radios[i].checked)
  {
  var valueSelected = radios[i].value;
  window.alert(valueSelected);
  SelectedValue.push("valueSelected")
  }
}
  var url = ['http://' + location.host, 'new_surveys', 'submit'].join('/');
  var callback = function(responseJSON, xhr) {
}
   create_ajax_request(url, {arrays: SelectedValue}, callback);
}

here is my controller, when i searched i found that the problem should be solved when i add this line : skip_before_filter :verify_authenticity_token, only: [:submit] But nothing new :/

class NewSurveysController < ApplicationController
  before_action :set_new_survey, only: [:show, :edit, :update, :destroy, :submit]
  skip_before_filter :verify_authenticity_token, only: [:submit]

# GET /new_surveys
# GET /new_surveys.json
def index
  @new_surveys = NewSurvey.all
end

def submit
  data = params[:arrays]
  some code ..
end

log

 Started PUT "/new_surveys/submit" for 127.0.0.1 at 2014-05-15 19:26:12 +0200
Processing by NewSurveysController#update as JSON
Parameters: {"arrays"=>["valueSelected"], "id"=>"submit", "new_survey"=>{}}
Can't verify CSRF token authenticity
Completed 422 Unprocessable Entity in 15ms

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
actionpack (4.0.4) lib/action_controller/metal/request_forgery_protection.rb:166:in `handle_unverified_request'
actionpack (4.0.4) lib/action_controller/metal/request_forgery_protection.rb:173:in `handle_unverified_request'
devise (3.2.4) lib/devise/controllers/helpers.rb:182:in `handle_unverified_request'
Ahmed Bassiouny
  • 276
  • 6
  • 25
  • are you saying that you found the solution? If not, can you post your log from the webserver output? – Jake Smith May 15 '14 at 17:02
  • no i didnt , and i updated my Question , thanks in advance – Ahmed Bassiouny May 15 '14 at 17:28
  • If you haven't already, take a look at the suggested answers [here](http://stackoverflow.com/questions/3364492/actioncontrollerinvalidauthenticitytoken). The selected answer isn't much help, but the other suggestions seem to be a good list to try out. – Jake Smith May 15 '14 at 17:47
  • I'm experiencing the same problem. With POST and DELETE request. But not all of them, only once upon a time, both in development and production. I'm on Rails 4.1 and of course I'm using gem 'jquery-rails'. I'm going to try to add this http://weblog.rubyonrails.org/assets/2011/2/8/jquery-snippet.js. But don't like it as it should be already provided by the mentioned gem. – Tomas Gregor Jul 31 '14 at 10:09
  • I had a similar issue and [this answer][1] worked for me. [1]: http://stackoverflow.com/questions/14734243/rails-csrf-protection-angular-js-protect-from-forgery-makes-me-to-log-out-on – Ben Morris Sep 30 '14 at 21:04

0 Answers0