我需要我的服務從服務器返回帶有信息的json。無法在AngularJS服務中返回對象類型
服務代碼:
app.service('Server', function() {
this.fetch = function (data, token) {
fetch('http://104.197.58.108:8080', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
data: data,
token: token
})
})
.then(function(response) {
return response.json()
}).then(function(text) {
if (text.success == false) {
return false;
} else {
return text;
}
})
}
})
下面是來自控制器代碼。
$scope.data2 = '{"func":"materialSons","material":"0"}';
console.log(Server.fetch($scope.data2, $rootScope.token));
$scope.materialist = Server.fetch($scope.data2, $rootScope.token);
請求服務器成功,但功能返回未定義。 我也嘗試添加return語句之前取()的服務,而且我在日誌了:
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
爲獲取方法提供回調函數 –