簡體   English   中英

AngularJS將api請求從php更改為JSON

[英]AngularJS Change api request from php to JSON

嗨,我有這個可以連接到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的基本方法

需要從以下位置更改控制器:

$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 它與文件是JSONPHP無關

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM