使用Restangular,get方法/ promise会解析,但传递给.then()的结果为空...使用console.log(data);显示未定义。我检查了chrome debug中的网络选项卡,并且xhr请求很好,成功了200 ...正文中有一个完整的json响应。
使用addResponseInterceptor
,我发现data参数未定义,但response参数显示包含带有负载/ body的data属性的对象。
所以,我还在挠头......为什么数据参数未定义,而响应对象在response.data中正确包含json有效负载/响应?
我需要解决这个问题,以便在解析时将结果传递给.then()。
createNode: function(node) {
var account = "9936";
var eventId = "0fd6afd9-4aa0-a5c9-ff0b3e60cdcf";
Restangular.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
console.log("````````````````");
console.log(data);
console.log(operation);
console.log(what);
console.log(url);
console.log(response);
console.log(deferred);
console.log("````````````````");
});
node.js:12
undefined node.js:13
get node.js:14
event node.js:15
/1.0/loadbalancers/account/9936/event/0fd6afd9-4aa0-a5c9-ff0b3e60cdcf node.js:16
^Object {data: Object, status: 200, headers: function, config: Object}
config: Object
data: Object
Automation: Object
Classification: Array[2]
ExecutionTimestamp: "2014-08-13T16:08:37.4676Z"
ID: "0fd6afd9-a5c9-ff0b3e60cdcf"
Incident: Object
LastStatus: "ENQUEUED"
Metadata: Object
Name: "Node Create"
RecordLogs: false
Source: "Device Lifecycle"
Stream: Object
Targets: Array[1]
答案 0 :(得分:6)
确保所有响应拦截器最后都返回数据:
Restangular.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
// your code here
return data;
});
在以下响应拦截器调用中,不返回任何内容会将数据设置为undefined
。