【问题标题】:Not getting json data from json-angularjs call to an external url with $http.get using success method没有使用 $http.get 使用成功方法从 json-angularjs 调用外部 url 获取 json 数据
【发布时间】:2017-12-31 07:49:21
【问题描述】:

Controller.js 文件代码:

var myApp=angular.module('myApp',[]);

myApp.controller('myController', function($scope,$http){

    $http.get('data.json').success(function(data){
        $scope.art=data;
    }); 
});

【问题讨论】:

  • $scope.art=data;用响应替换数据

标签: javascript angularjs json model-view-controller


【解决方案1】:

您必须将响应分配给$scope.art

var myApp=angular.module('myApp',[]);

myApp.controller('myController', function($scope,$http){

    $http.get('data.json').success(function(response){
        $scope.art=response;
    }); 
});

注意:已弃用的 .success 和 .error 方法已从 AngularJS 1.6 中删除

使用.then

var myApp=angular.module('myApp',[]);

myApp.controller('myController', function($scope,$http){

    $http.get('data.json').then(function(response){
        $scope.art=response.data;
    }); 
});

【讨论】:

  • 你的角度版本是什么,你的回复包含什么?
  • 显示如下错误:TypeError: $http.get(...).success is not a function
  • 这是因为您使用的是较新版本的 angularjs,其中 .success 和 .error 方法已被弃用。而是使用 .then
【解决方案2】:
var myApp=angular.module('myApp',[]);

myApp.controller('myController', function($scope,$http){

    $http.get('data.json').then(function(response){
        $scope.art=response.data;
    }); 
});

试试上面的。可能有帮助!

【讨论】:

  • 很高兴我能提供帮助。
猜你喜欢
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多