简体   繁体   中英

React-rails store POST request response

currently i used Ruby on Rails as my backend to serve API to my apps. And my front end i use React-rails gem (React.js) to get/post data and generate output. Both API and apps are on different server.

My problem here is, i cannot store response from POST request i made in react. How can i do so ? I am new in react btw.

What i want to do now is when user want to login the apps will POST request to API and get "auth_token" response. I want to store this "auth_token" response in header and access into dashboard. But i stuck now how can i store the auth_token ftw.

Below is my code for POST data :

 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: [] } }, 

If there's someone that could solve my problem and teach me how can i store in header in my apps i really appreciate it.

Just store them in local storage like:

localStorage.authToken = data.auth_token

And then whenever you need them in your future requests, just read as:

localStorage.authToken

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM