サーバからの情報を含む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}
fetchメソッドにコールバック関数を提供します。 –