html5_AngularJs访问网络数据ajax

本文展示了一个使用AngularJS和jQuery进行数据加载的例子。通过$http服务从JSON文件中获取书籍信息,并显示在网页上。此外,还展示了如何通过$http.get发起GET请求来获取外部API的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="angular-1.3.0.js"></script>
    <script type="text/javascript" src="jquery-1.11.0.js"></script>
    <script>
        var app = angular.module("myapp",[]);
        app.controller("myctrl",function($scope, $http){
            $http({
                method: "GET",
                url: "book.json"
            }).success(function(data, status, headers, config){
                $scope.book = data;
            }).error(function(data, status, headers, config){
            });

            $http({
                method: "GET",
                url: "books.json"
            }).success(function(data, status, headers, config){
                $scope.books = data;
            }).error(function(data, status, headers, config){
            });          
            //get请求
            $http.get("https://free-api.heweather.com/v5/weather?city=beijing&key=545d63e185fc48169a43cbabba6e74d2").then(function (response) {
                $scope.book=response.data;
            }, function (response) {
               console.log(response.status);
            });
});
    </script>
</head>
<body ng-app="myapp" ng-controller="myctrl">   
     <ul>        
       <li>ID:{{book.id}}</li>
       <li>标题:{{book.title}}</li>
       <li>图片:<img src="{{book.picture}}" style="width: 100px;height: 100px"></li>
     </ul>
 
  <table border="1" cellpadding="10" cellspacing="0">
     <tr>
         <td>ID</td>
         <td>标题</td>
         <td>图片</td>
     </tr>      
    <tr ng-repeat="book in books">
          <td>{{book.id}}</td>
          <td>{{book.title}}</td>
          <td><img src="{{book.picture}}" style="width: 100px;height: 100px"></td>
      </tr>  
</table>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值