【问题标题】:AngularJS Change api request from php to JSONAngularJS 将 api 请求从 php 更改为 JSON
【发布时间】: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 的基本方式

【问题讨论】:

    标签: php angularjs json api


    【解决方案1】:

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

    $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 的文件有关

    【讨论】:

      猜你喜欢
      • 2017-10-31
      • 1970-01-01
      • 2013-06-30
      • 2018-01-03
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      相关资源
      最近更新 更多