2017-05-23 136 views
0

我收到了我的this.http.post的回复。下面是我的代码需要在本地存储中保存和检索Http post response

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

回答

1

试试下面的代码添加响应localStorage的

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

这是我得到我的js代码的结果 - https://pastebin.com/tb1dGA1f,如何成功和获取data.user使用,如在这样的js中 - “$ .ajax({0} 0%{url = 'http://funiks.com/adminv7/offline-api/login.php', type:'POST', contentType:'application/json;字符集= UTF-8' , 数据类型: 'JSON', 数据:JSON.stringify(字典), }) .done(功能(数据,textStatus,jqXHR){ \t如果(data.status ==” SUCCESS“)localStorage.setItem(”getLoggedInUser“,JSON.stringify(data.user));'但在角度我得到这个 - https://pastebin.com/W49NAQvz,如何获得相似的结果?? – user2828442

+0

使用'JSON.parse(response._body).user',因为响应正文是一个字符串,而不是一个对象 – HARI

+0

但我不能保存我使用html5或js保存的东西,角度我在本地节省了太多不需要的东西存储,我只想保存与使用我的html5方法保存类似的东西,请指导我如何做到这一点 – user2828442

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. 
+0

这就是我用我的js代码 - pastebin.com/tb1dGA1f得到的结果,如果成功并获取data.user,就像这样在js中使用 - $ .ajax({url:'http:// funiks。 com/adminv7/offline-api/login.php',输入:'POST',contentType:'application/json; charset = utf-8',dataType:'json',data:JSON.stringify(dict),}).done(function(data,textStatus,jqXHR){if(data.status ==“SUCCESS”){localStorage。 setItem(“getLoggedInUser”,JSON.stringify(data.u ser));但在角我得到这个 - pastebin.com/W49NAQvz,如何得到相似的结果?? – user2828442

+0

我认为你的API不会返回任何数据 –

+0

我附上了一个截图,它还附上了pastebin链接,你看到了吗?..再次为你添加--->带角度结果 - pastebin.com/W49NAQvz,结果为js - pastebin.com/tb1dGA1f – user2828442