React-rails 存储 POST 请求响应

React-rails store POST request response

目前我在 Rails 上使用 Ruby 作为我的后端服务 API 我的应用程序。我的前端使用 React-rails gem (React.js) 到 get/post 数据并生成输出。 API 和应用程序都在不同的服务器上。

我的问题是,我无法存储来自 POST 我在 React 中提出的请求的响应。我该怎么做?顺便说一句,我是 React 的新手。

我现在想做的是,当用户想要登录时,应用程序将 POST 请求 API 并获得 "auth_token" 响应。我想将此 "auth_token" 响应存储在 header 中并访问仪表板。但我现在卡住了,我该如何存储 auth_token ftw。

下面是我的 POST 数据代码:

var LoginBody = React.createClass(
{
    handleClick() {
        var username  = this.refs.username.value;
        var password = this.refs.password.value;
        $.ajax({
            url: 'http://localhost:3001/api/v1/users/central/signin?',
            type: 'POST',
            dataType: 'json',
            data: { user_login:{email: username, password:password} },
            success: (data) => {
                 this.setState({auth_token: data.auth_token});
            }.bind(this),
        });
    },
    getInitialState(){
      return {
        auth_token: []
      }
    },

如果有人可以解决我的问题并教我如何在我的应用程序中存储 header 我真的很感激。

只需将它们存储在本地存储中,例如:

localStorage.authToken = data.auth_token

然后当您在以后的请求中需要它们时,只需读作:

localStorage.authToken