`

jQuery Ajax calls in Rails 3 getting 401 Unauthorized Request

阅读更多
Problem:


    Sign in
    Sign out
    Sign in (successful 201. However prints WARNING: Can't verify CSRF token authenticity in server logs)
    Subsequent ajax request fails 401 unauthorised
    Refresh the website (at this point, CSRF in the page header changes to something else)
    I can sign in, it works, until I try to sign out and in again.


Solution:


protect_from_forgery :except => :rate


$.ajax({ url: 'YOUR URL HERE',
  type: 'POST',
  beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
  data: 'someData=' + someData,
  success: function(response) {
    $('#someDiv').html(response);
  }
});


<%= csrf_meta_tag %>

$(document).ajaxSend(function (e, xhr, options) {
  xhr.setRequestHeader("X-CSRF-Token", MyApp.session.get("csrf-token"));
});


signOut: function() {
  var params = {
    dataType: "json",
    type: "GET",
    url: this.urlRoot + "/sign_out.json"
  };
  var self = this;
  return $.ajax(params).done(function(data) {
    self.set("csrf-token", data.csrfToken);
    self.unset("user");
  });
}
分享到:
评论

相关推荐

    jquery电子文档chm

    Attach a function to be executed whenever an AJAX request completes successfully. This is an Ajax Event. The XMLHttpRequest and settings used for that request are passed as arguments to the callback. ...

    Asynchronous Procedure Calls in NT

    Asynchronous Procedure Calls ,也就是异步过程调用,这份文档是内核中APC的详尽介绍,值得一看

    Remote Function Calls in ABAP

    SAPTEC training course

    jQuery Design Patterns(PACKT,2016)

    jQuery solves the problems of DOM manipulation, event detection, AJAX calls, element selection and document queries, element attribute and data management, as well as object management utilities....

    ASP.NET AJAX in Action

    1 ■ Introducing ASP.NET AJAX 3 2 ■ First steps with the Microsoft Ajax Library 36 3 ■ JavaScript for Ajax developers 73 4 ■ Exploring the Ajax server extensions 114 5 ■ Making asynchronous ...

    jQuery.Design.Patterns.178588

    jQuery solves the problems of DOM manipulation, event detection, AJAX calls, element selection and document queries, element attribute and data management, as well as object management utilities....

    Tracing Oracle Internal Calls

    Tracing Oracle Internal Calls

    前端项目-jquery.iframe-transport.zip

    前端项目-jquery.iframe-transport,jQuery plugin that implements an iframe transport so that ajax calls support the uploading of files using standard HTML file input fields

    Learning PHP, MySQL & [removed] With jQuery, CSS & HTML5 ER5

    jQuery mobile librariesUse Ajax calls for background browser.server communicationAcquire CSS2 & CSS3 skills for professionally styling your web pagesImplement powerful HTML5 features, including ...

    ajaxCalls:Ajax 调用示例

    ajax调用 Ajax 调用示例 感谢 Tuts 的帮助。 我最喜欢的资源之一。

    【原创】Accurately Profiling Direct3D API Calls (Direct3D 9)总结

    阅读 《Accurately Profiling Direct3D API Calls (Direct3D 9)》之后,本人做的总结

    AsyncCalls(异步调用函数)

    AsyncCalls(异步调用函数),Delphi异步调用函数的单元和例子

    Ajax 探密

    What it calls for is Ajax Hacks from O'Reilly. This valuable guide provides direct, hands-on solutions that take the mystery out of Ajax's many capabilities. Each hack represents a clever way to ...

    The Definitive Guide to JSF in JavaEE8 2018

    such as using WebSockets, invoking bean methods directly from Ajax, executing client-side JavaScript when Ajax calls complete, and more Along the way you’ll broaden your knowledge of JSF ...

    JavaScript Web Service Calls

    这东西比你想象的要难找,用的时候,怎么也找不到,闲来放出来

    Ajax-calls-to-load-data-from-json-file

    我已经使用Jquery作为客户端框架,这有助于对服务器进行Ajax调用并将json数据加载到网页。 Node.js具有丰富的库函数集合,可以轻松托管服务器。 使用jQuery对服务器进行Ajax调用是一项棘手的任务,并且花费了一些...

    CAMEL - Intelligent Networks for GSM, GPRS, UMTS Networks

    1.1 Signalling in GSM 3 1.2 GSM Mobility 3 1.3 Mobile Station 4 1.4 Identifiers in the GSM Network 4 1.4.1 International Mobile Subscriber Identity 4 1.4.2 Mobile Station Integrated Services Digital ...

    servlet2.4doc

    The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. doGet...

Global site tag (gtag.js) - Google Analytics