从响应中获取数据

时间:2017-01-20 14:40:12

标签: javascript angularjs

我的回复是以下格式,

@Component({
  template: `
    <child-comp (buttonClicked)="handleAction($event)"></child-comp>
  `
})
export class ParentComponent  {

  handleAction(actionName) {
    // Here, put some logic to call a local method based on `actionName`
    // Something like this[actionName]();
  }

  actionOne() {
      ...
  }

  actionTwo() {
      ...
  }

}

我不确定如何访问{ "access_token": "eWcWLctGW-_NgGVAmFbO9l-nt3yztFzlZCLLlilI9mGDcM5q8d0kQw0uzvFOoXynHcb-MuPVJGTGkSkBhrr69_-aN1r5j9zB4fCl4u4aqOQ-scNI36xgHeGYpXky60drIBpMI83FGqd9pMjL4GWXjFHq61nhJ6xkGj1u1r9a5u6EJrB1lfjNhljzC_j65xaqxtubQ4AglKFO2ib-levpvnd_bEU-QGQrtvS2QbaXhb_hlnX8czo61Gn_OQyBVk7HbN1SozxIPe3RBvf5AiCAouDMz1WMHy9ybVFy8SnoNIgszjo7Ev2IEWS9aFb87u6bvoJvSVJv7s3z-2GUvG2kwfOk2sUWrmq0QeIJJrYwdKQfs3T8HrK2MNKSGteJ04-O", "token_type": "bearer", "expires_in": 1799, "refresh_token": "f1005c7fd74247069dbdb078ee379410", "as:client_id": "438dc832-33c7-413b-9c71-d0b98a196e6a", "userName": "master", ".issued": "Fri, 20 Jan 2017 14:30:09 GMT", ".expires": "Fri, 20 Jan 2017 15:00:09 GMT" }.issued.expires

我正在使用angular并传递用户名,密码和company_id,并以上述格式获取响应。

as:client_id

我只需使用dataService.getAuthToken($scope.username, $scope.password, $scope.company_password).then(function (response) { //response data here });即可轻松获取token_type,access_token但不确定如何访问response.data.access_token.issued.expires

1 个答案:

答案 0 :(得分:5)

您可以通过索引器语法访问JavaScript中对象的每个属性,就像它是一个地图一样(因为对象是javascript中的一个地图):

var issued = response.data[".issued"];
var expires = response.data[".expires"];
var asClient_id= response.data["as:client_id"];

请参阅此链接:http://www.w3schools.com/js/js_objects.asp

  

访问对象属性
  您可以通过两种方式访问​​对象属性:

     

objectName.propertyName
  或
  对象名[ “PROPERTYNAME”]