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 8283887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:54:07+00:00 2026-06-08T10:54:07+00:00

When I try to set my request header token, I receive an error: ActionController::RoutingError

  • 0

When I try to set my request header token, I receive an error:

ActionController::RoutingError (No route matches [OPTIONS] "/data"):

Here is the ajax call:

  $.ajax({
    url: this.hostName + url,
    type: 'POST',
    data: data,
    dataType: 'json',
    beforeSend: function( xhr ) {
      xhr.setRequestHeader( 'X-CSRF-Token', $( 'meta[name="csrf-token"]' ).attr( 'content' ) );
    },
    success: function(response) {
      console.log('success');
      console.log(response);
    },
    error: function(response) {
      console.log('error');
      console.log(response);
    }
  });

If I leave the request header out:

Started POST "/data" for 127.0.0.1 at 2012-07-24 18:37:22 -0700

but I get a warning stating:

WARNING: Can't verify CSRF token authenticity

Any ideas as to why this is happening?

  • 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. Editorial Team
    Editorial Team
    2026-06-08T10:54:08+00:00Added an answer on June 8, 2026 at 10:54 am

    Have you tried

    $.ajax({
        url: this.hostName + url,
        type: 'POST',
        headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') }
        data: { param : 'something' },
        dataType: 'json'
    });
    

    And then in your controller params[:param] will give you ‘something’.

    If you are interested to know why you need all that X-CSRF-Token stuff, this is a good read. Basically, Rails automatically includes that token in your page to keep your application safe. If you inspect any page of your site, you’ll see in the head of your HTML document something like this:

    <meta content="guma1QdmO9Tn9SB4yV4DonkY4xf4Sy6lIvrFyHIaR1U=" name="csrf-token">
    

    This is created by the line <%= csrf_meta_tags %> included in your application.html.erb file.

    Rails automatically includes this token in regular non-GET requests to keep them safe. But with javascript and ajax, you have to do this manually by searching the DOM for the token with the jQuery function $('meta[name="csrf-token"]'.

    Now, this isn’t very efficient because you are searching for that token every time you are making a request. So what you should do, is use ajaxSetup, like this:

    $.ajaxSetup({
        type: 'POST',
        headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') }
        dataType: 'json'
    });
    

    And now, whenever you want to send a request, all you need is this:

    $.ajax({
        url: this.hostName + url,
        data: { param : 'something' }
    });
    

    EDIT: Ah, I see. What do you get when you do alert(this.hostName + url)? And how are you declaring your routes? Because if I remember correctly, you can use relative urls instead of absolute urls, you don’t need the root part. Here’s an example:

    # routes.rb
    post "relative_url" => 'controller#action'
    
    # file.js
    $.ajax({
        url: "relative_url",
        data: { param : 'something' }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What parameters on a $.ajax must I set to try and mask the AJAX-request
try to set host in dsn of pdo like this: <?php /* Connect to
I try to set friendship to method from class GameSimulator on Player class. for
I try to set a timeout on an element, fired with a jQuery plugin.
I try to set an attribute in a XML node using MSXML. IXMLDOMElement alone
I try to set up .hgrc file: [ui] username = MyName MySecondName <mymail@gmail.com> I
Every time try to set the value of any variable in my model object,
I have try to set icon for my App in the xCode. In the
When I try and set the datatable width just the table main section is
I will try and set the scene as best i can. *lights candle What

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.