为了账号安全,请及时绑定邮箱和手机立即绑定

AngularJS,如何从API获取数据并显示

AngularJS,如何从API获取数据并显示

温温酱 2023-09-18 15:18:24
我试图学习角度JS。我正在尝试从以 JSON 格式提供艺术家数据的 API 中获取数据。我正在尝试做的是获取在文本栏中输入的名称,然后仅将收到的信息显示为 JSON。 API 接受的格式是 url/艺术家名称 + 应用 ID 作为请求,其中艺术家名称是输入。我尝试使用的 API 的人给了我一个有效的应用程序代码,该代码存储在 var id 中。 到目前为止,我已经编写了以下代码,任何帮助将不胜感激。谢谢!    var myApp = angular.module('myApp', []);    myApp.controller('UserCtrl', function($scope,$http){        $scope.user = null;        var id = "my secret id comes here";        var name = $scope.searchText;        $scope.getByID = function() {            $http.get("https://rest.bandsintown.com/artists/" + name + id)                    $scope.user = response;                    console.log(response)                }              });接下来,html 代码是:<body ng-app="myApp">    <div ng-controller="UserCtrl">        Search : <input type="text" placeholder="Search Employees" ng-model="searchText"/> <br/><br/>        <button ng-click="UserCtrl.getByID()">Submit</button></body>谢谢,对不起,如果这是一个新手问题,我确实是一个新手!
查看完整描述

1 回答

?
慕少森

TA贡献2019条经验 获得超9个赞

response没有在任何地方声明,所以看起来你只需要正确处理响应。以下方法应该有效:$http.get


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

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

   $scope.user = null;


   var id = "my secret id comes here";

   var name = $scope.searchText;


   $scope.getByID = function() {

     // $http.get returns a promise, you'll need to set the variables there.

     $http.get("https://rest.bandsintown.com/artists/" + name + id)

       .then(response => {

         $scope.user = response;

         console.log(response)

       })

   }

 });


查看完整回答
反对 回复 2023-09-18
  • 1 回答
  • 0 关注
  • 36 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信