title: SyntaxError: Unexpected token u in JSON at position 0 in Angular2 when try to store in local storage
body: <p>I have a json response like this.</p>
  
  <pre><code>   {
    "response": {
      "data": {
        "name": "ABC",
        "Id": "1234",
        "address": "adthhyrr"    
      }
    }
  }
  </code></pre>
  
  <p>In service.ts I am doing a request like this to get the response data. </p>
  
  <pre><code> public getResponseData() : Promise&lt;any&gt; {
        if(typeof(this.login_data) === "undefined") {
              return this.http.get('assets/response.json')
              .toPromise().then(res =&gt; {
                                    this.login_data = res.json().response.data;
  
                                    return this.login_data;
                  }).catch(this.handleError);
        } else {
            return Promise.resolve(this.login_data);
        }
  }
  </code></pre>
  
  <p>And I am storing the login_data in localstorage like this</p>
  
  <pre><code>  getAllData() {  
      if (localStorage.getItem('login_data') === null || localStorage.getItem('login_data') === undefined) {  
        localStorage.setItem('login_data', JSON.stringify(this.login_data)); 
        return this.login_data;  
      }  
      else {  
        var login_data = JSON.parse(localStorage.getItem('login_data'));  
        return login_data;  
      }  
  
    } 
  </code></pre>
  
  <p>And in component.ts class I am getting data like this</p>
  
  <pre><code>   this.logindata = this.myservice.getAllData();
  </code></pre>
  
  <p>But while doing this I am getting this error.
  <strong>SyntaxError: Unexpected token u in JSON at position 0</strong>.</p>
  
  <p><strong>But if i am not storing in localstorage then i am doing like this in service.</strong></p>
  
  <p>public getAllData(): any {</p>
  
  <pre><code>return this.login_data;
  </code></pre>
  
  <p>}</p>
  
  <p>And in component.ts file i am getting data by calling this method.</p>
  
  <pre><code> this.logindata = this.myservice.getAllData();
  </code></pre>
  
  <p>And here it is working fine.But when i am trying to store data into localstorage and want to fetch from local storage i am getting this error.
  <strong>SyntaxError: Unexpected token u in JSON at position 0</strong></p>
  
  <p>Can anyone please tell me where exactly i am doing wrong.</p>