【发布时间】:2019-06-02 11:41:58
【问题描述】:
您好,我有这个可以连接到 API 的应用。
唯一的问题是它只能来自PHP 文件的get 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 的基本方式
【问题讨论】: