嗨,我有这个可以连接到API的应用程序。
唯一的问题是它只能get
文件发出PHP
API请求。
我想更改代码,使其可以接受.json
API文件
工厂
factory.readProducts = function(){
return $http({
method: 'GET',
url: 'https://jsonplaceholder.typicode.com/todos'
});
console.log(factory.readProducts)
};
目前应该将我的GET
方法更改为fetch
控制器
$scope.readProducts = function(){
// use products factory
productsFactory.readProducts().then(function successCallback(response){
$scope.todos = response.data.records;
console.log($scope.products)
}, function errorCallback(response){
$scope.showToast("Unable to read record.");
});
}
JSON方法
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))
根据JSONPlaceholder,这是访问JSON API的基本方法
答案 0 :(得分:0)
需要从以下位置更改控制器
$scope.readProducts = function(){
// use products factory
productsFactory.readProducts().then(function successCallback(response){
$scope.todos = response.data.records;
console.log($scope.products)
}, function errorCallback(response){
$scope.showToast("Unable to read record.");
});
}
收件人:
$scope.readProducts = function(){
// use products factory
productsFactory.readProducts().then(function successCallback(response){
$scope.todos = response.data.records;
console.log($scope.products)
}, function errorCallback(response){
$scope.showToast("Unable to read record.");
});
}
由于API的结构,$scope.todos = response.data.records
需要更改为$scope.todos = response.data
。它与文件JSON
或PHP