Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 181777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:49:24+00:00 2026-05-11T14:49:24+00:00

I have some javascript making an ajax call in my Rails site: $.ajax({type: PUT,

  • 0

I have some javascript making an ajax call in my Rails site:

$.ajax({type: 'PUT', url: url, data: { dummy: data }, complete: function(data) {}}); 

When Rails gets it, it throws back an ActionController::InvalidAuthenticityToken Error. I’d like to keep the protect_from_forgery stuff in there, if possible… But I’m at a loss for how can I pass the auth token from a javascript file?

Can anyone help me out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:49:24+00:00Added an answer on May 11, 2026 at 2:49 pm

    In your layout, add this before any other JS runs:

    <script>   function authToken() {     return '<%= form_authenticity_token if protect_against_forgery? -%>';   } </script> 

    authToken is coded as a function so that it’s less likely you’ll accidentally overwrite it with other JavaScript.

    Alternatively, as of Rails 3, the auth token is embedded as a <meta> tag, which you can read with:

    <script>   function authToken() {     return $('meta[name='csrf-token']').attr('content');   } </script> 

    In your main JS, you can then call authToken(), and it’ll return your authenticity token as a string to include in your Ajax calls. For example, using jQuery:

    $.ajax({   type: 'PUT',   url:  url,   data: {     foo: bar,     authenticity_token: authToken()   },   complete: function(data) {} }); 

    Note that if you use Rails’ built-in form_for helper, it automatically adds the authenticity token in a hidden input. If you want to send all of the form’s data, including the hidden auth token, you can simply use:

    var $form = $('form'); $.ajax({   url:      $form.attr('action'),   type:     $form.attr('method'),               // 'get' or 'post'; overridden by Rails' hidden '_method'               // input value, e.g., 'put'   data:     $form.serialize(),               // Includes hidden 'authenticity_token' and '_method' inputs   complete: function(data) {} }); 

    This pattern is often useful when you’ve already written a form that works without JS, and you’re adding an unobtrusive layer of JS that simply sends the form’s data via Ajax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 301k
  • Answers 301k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It looks like your join to Designation isn't even used… May 13, 2026 at 8:16 pm
  • Editorial Team
    Editorial Team added an answer Something similar was asked today for a shell script. The… May 13, 2026 at 8:16 pm
  • Editorial Team
    Editorial Team added an answer I guess what they want you to do is precisely… May 13, 2026 at 8:16 pm

Related Questions

I have some jQuery code in an external Javascript file that is making an
I am probably making some silly mistake but I am newer to the world
I am having some trouble with the classic javascript local variable scope topic, but
I'm about to start a fairly Ajax heavy feature in my company's application. What

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.