JavaScript Rails 4 trying to parse POST JSON in controller

I have a controller with the path to create a row in my db

The page model.js has a function that does an ajax post

function autoSaveVideo(playPos){
        $.ajax({
            type: "POST",
            url: "/video_views/create",
            dataType: "json",

            data: { 
                play_position: 0,
                user_id: $('.user_id').text(),
                video_id: $('.video_id').text()
            },
            error: function (data) {
                console.log(data);
                return data
            }
        });
        setTimeout(autoSaveVideo, 6000);

I then have the controller

render :nothing => true, :status => 200, :content_type => :json

this sends a successful 200 response

but if I add to the video_views_controller.rb <-- edit and note: not Videos controller

jData = JSON.parse(request.body.read)

I get an 795: unexpected token, in the console

how do I parse it so that I can access the data object?

Answer:1

Pardeep is almost correct. You'll need to use params method to access the values in the data object you sent. So params['play_position'], etc.

It is a security best practice to explicitly list the keys you're expecting and permitting. See Rails Guide for strong parameters.

http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters

Answer:2

This is more of a code style question. In ES6, we can define arrow functions like so let hello = () => console.log('world') We can also replace () with _ to save one character, but jslint warns ...

This is more of a code style question. In ES6, we can define arrow functions like so let hello = () => console.log('world') We can also replace () with _ to save one character, but jslint warns ...

I am trying to run a script in a new tab. The code I use is this: $ = jQuery; function openAndPush(url, id) { var win = window.open('https://example.com' + url + '?view=map'); var element = $(...

I am trying to run a script in a new tab. The code I use is this: $ = jQuery; function openAndPush(url, id) { var win = window.open('https://example.com' + url + '?view=map'); var element = $(...

I have a function that catches whether the input has changed or not then if the value is exceeding the max it sets the value to the max. So you can not enter a value over the max. But if you try to do ...

I have a function that catches whether the input has changed or not then if the value is exceeding the max it sets the value to the max. So you can not enter a value over the max. But if you try to do ...

I have Set() object with three key value var myset =new Set(); myset.add('first','This is first value'); myset.add('second','This is second value'); myset.add('third','This is third value'); Using ...

I have Set() object with three key value var myset =new Set(); myset.add('first','This is first value'); myset.add('second','This is second value'); myset.add('third','This is third value'); Using ...

  1. individual value at risk
  2. individual value of privacy
  3. individual value in excel
  4. individual value of friendship
  5. individual value of human life
  6. individual value of
  7. individual value in english