Skip to content Skip to sidebar Skip to footer

React Js Error Unexpected End Of Json When Passing User Token Via Session-storage

I have a login page, which excepts the username and password. Upon button submission, the page processes the user credentials via a json / api and generates a token thus routing th

Solution 1:

You need to stringify the data into sessionStorage. Otherwise it's going to look like this "[object Object]". This is because localStorage and sessionStorage can only store DOMStrings and not objects. The documentation for Storage.setItem

Simple test to verify:

sessionStorage.setItem('token', {'a':1})
let t = sessionStorage.getItem('token')
// "[object Object]".

Solution 2:

As Henrik has posted, you will have to set the value to local Storage as,

sessionStorage.setItem('token', JSON.stringify(data))

and use it as,

var token = JSON.parse(sessionStorage.getItem('token'));

Post a Comment for "React Js Error Unexpected End Of Json When Passing User Token Via Session-storage"