javascript-从http内部函数返回数据
作者:互联网
我正在使用this和this引用,以从函数内的http请求返回数据:
function getdetails(id) {
var datafordetails = {
data1: {
item1: "",
item2: ""
},
data2: {
item3: "",
ID: id
}
};
var jsonDataDetails = JSON.stringify(datafordetails);
return $http({
url: "http://...",
method: "POST",
data: jsonDataDetails,
dataType: "json",
timeout: 5000,
contentType: 'application/json; charset=utf-8'
})
.then(function (res) {
var data = res.data;
responsedata=data.d;
return responsedata;
},function (error) {
$scope.status = status;
console.log("Unable to update. No internet?");
})
}
我的目标是var testing = getdetails(‘12345’);给我该ID的响应数据,但我回到了Promise {$$state:Object …
我究竟做错了什么?非常感谢!
解决方法:
您获得的输出不过是$http.get方法返回的promise对象.基本上,您需要将.then函数放在getdetails2函数上,以便在promise对象解析/拒绝时从其获取数据.
码
getdetails2('12345').then(function(data){ //success callback
//you will get data here in data parameter of function,
var testing = data;
}, function(error){ //error callback
console.log(error)
})
标签:angularjs,asynchronous,promise,javascript,angular-promise 来源: https://codeday.me/bug/20191119/2034642.html