需要在本地存储中保存和检索Http post响应

时间:2017-05-23 10:54:51

标签: javascript arrays json angular post

我收到了this.http.post的回复。以下是我的代码

this.http.post('http://funiks.com/adminv7/offline-api/login.php', {username: user_form_value,password: password_form_value})
  .subscribe(
    (response)=> console.log(response)
  );

我想通过在本地存储中使用这样的保存响应,这是我在js中的操作,但请指导我如何保存角度和我需要放置代码的确切位置 -

 localStorage.setItem("getLoggedInUserANG",JSON.stringify(response));

请指导,同样如何将结果返回变量? 在js我喜欢这个并从数据中获取用户名

loggeduser_array  = JSON.parse(localStorage.getItem("getLoggedInUser"));
    console.log(loggeduser_array.username);

请在下面的控制台中查看回复: enter image description here

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码将响应添加到localstorage

.subscribe(
   response=>{
   localStorage.setItem("getLoggedInUserANG",JSON.stringify(response));
});

答案 1 :(得分:0)

localStorage不是js,它的HTML5功能

   const headers = new Headers();
    headers.append('Content-Type', 'application/json');
  var loginDetail = {username: user_form_value,password: password_form_value};

this.http.post('http://funiks.com/adminv7/offline-api/login.php', JSON.stringify(loginDetail),{headers})
  .subscribe(
    (response)=>
localStorage.setItem("getLoggedInUserANG",JSON.stringify(response));
console.log(response);
  );
//  Just get local storage value where you need.
loggeduser_array  = localStorage.getItem("getLoggedInUser");
    console.log(loggeduser_array.data); // you stored promise as response so while get try like this.