2016-01-11 36 views
0

我知道这应该很简单,但我是新来的项目,无法弄清楚如何得到这个返回的值。我试过“this”,“this.snippet”,message.whatever等等。举个例子来说,我如何从响应中获取properties.network_id?如何从外部类访问JSON响应 - Javascript

来电者:

v1.value.getSnippet(topology).then(function() { 
     console.log("snippet ->: " + JSON.stringify()); 
     //delete topology.extraData; 
    }); 

电话:

getSnippet: function(topology) { 
     if (!this.snippet) { 
      if (!this.promise) { 
       var self = this; 
       this.promise = xhr(uReleaseConfig.urls.rest + "/cloud/network/" + this.id, { 
        headers: { 
         "Accept": "application/json" 
        }, 
        handleAs: "text" 
       }).then(function (response) { 
        var data = JSON.parse(response || null); 
        self.snippet = data.message; 
       }, function(error){ 
        self.error = error; 
       }); 
      } 
      return this.promise; 
     } 
     return this.snippet; 
    } 

响应:

{"message":"{\"__name_placeholder__\":{\"type\": \"OS::Neutron::Net\", \"properties\": {\"network_id 
\": \"b2ac2526-ae8f-4d82-b1da-8d77b79f3476\", \"external\":\"false\", \"subnets\": [{\"id\":\"a1e3441e-4dcc-4c4a-ac12-2b60fe440c51 
\",\"name\":\"product-dev-subnet-ben\",\"cidr\":\"192.168.20.0\\\/24\"},{\"id\":\"e2e05590-5539-45bb-995e-8f565e6c16ff 
\",\"name\":\"product-dev-subnet\",\"cidr\":\"192.168.10.0\\\/24\"}] }}}\n"} 
+0

你确实意识到你的回应只是一个“”消息“的单个关键字,其值是一个很长的字符串,对吗?你需要解析这个值来获取'properties.network_id' –

+0

我并不太担心这个问题,但在记录“message”和“this.message”时仍然会得到“undefined” – benishky

回答

0

我需要在返回VAL一个变种:

.then(function(returnVal) 

console.log("snippet ->: " + JSON.stringify(returnVal));