`

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");
  });
}
分享到:
评论

相关推荐

    Remote Function Calls in ABAP

    本培训课程"Remote Function Calls in ABAP"旨在教授开发人员如何有效地利用RFC进行跨系统交互。 RFC是一种标准化的接口技术,使得ABAP程序能够调用远程系统中的函数模块,就像它们是本地函数模块一样。这在分布式...

    Manning jQuery in Action.pdf

    - **Ease of Use**: jQuery simplifies common tasks like manipulating the DOM, handling events, and making AJAX calls, which can otherwise be complex and error-prone when using plain JavaScript. ...

    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. ...

    ajax in prpc

    ### Ajax in PRPC #### 1. Dojo Introduction ##### 1.1 What is Dojo? Dojo is an open-source JavaScript toolkit designed to facilitate the construction of dynamic web user interfaces. This powerful ...

    Sap Ruby Ajax

    # SAP Ruby on Rails with AJAX: An In-depth Exploration ## Introduction The integration of SAP and Ruby on Rails has sparked considerable interest in the IT community, leading to the development of ...

    Asynchronous Procedure Calls in NT

    在Windows NT操作系统中,异步过程调用(Asynchronous Procedure Calls, APC)是一种重要的技术,用于在特定的线程上下文中执行任务,而无需线程持续监视或轮询。这种技术在Windows NT内核中扮演着重要角色,尤其是...

    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.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

    apex,ajax相关资料

    **Synchronous and Asynchronous Calls with the AJAX Toolkit:** - **同步调用(Synchronous Calls)**:这种类型的调用会阻塞UI,直到API调用完成。虽然简单易懂,但可能导致用户界面的冻结,影响用户体验。 - *...

    ajaxCalls:Ajax 调用示例

    **Ajax(Asynchronous JavaScript and XML)技术是一种在无需重新加载整个网页的情况下,能够更新部分网页内容的技术。在JavaScript中,Ajax调用常用于实现页面的异步数据交互,提高用户体验,减少用户等待时间。本...

    AsyncCalls(异步调用函数)

    3. **回调函数**:异步调用通常与回调函数一起使用,当异步任务完成时,回调函数会被调用,以便处理结果或执行其他逻辑。 4. **线程安全**:由于异步操作可能在不同的线程中执行,因此AsyncCalls单元可能包含一些...

    Modelling and Generating Ajax Applications A Model-Driven Approach

    AJAX (Asynchronous JavaScript and XML) represents a significant shift in the way interactive web applications are designed and developed. Traditional web applications rely heavily on the concept of ...

    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 ...

    (calls.zip) calls.tar.Z

    3. `calls.tar.Z`:这是一个压缩文件,名字暗示它可能包含了calls工具的原始源码或者未编译的版本。`.tar`是Unix/Linux下的归档格式,用于打包多个文件或目录;`.Z`则表示该归档文件使用了古老的Lempel-Ziv数据压缩...

    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 ...

    JavaScript Web Service Calls

    7. **JavaScript Web Service Calls.csproj**: 这是一个Visual Studio项目的文件,包含了项目的配置和引用信息,指示了整个项目是如何组织和构建的。 总的来说,这个项目涉及了使用JavaScript从客户端调用.NET编写...

Global site tag (gtag.js) - Google Analytics