[乐意黎转载]AngularJS快速入门指南09:SQL
发布时间:2020-12-17 09:36:12 所属栏目:安全 来源:网络整理
导读:我们可以将之前章节中的代码用来从数据库中读取数据。 通过PHP Server从MySQL数据库中获取数据 div ng-app ="myApp" ng-controller ="customersCtrl" table tr ng-repeat ="x in names" td {{ x.Name }} / {{ x.Country }} tr div script var app = angular.
我们可以将之前章节中的代码用来从数据库中读取数据。 通过PHP Server从MySQL数据库中获取数据<div ng-app="myApp" ng-controller="customersCtrl"> table> tr ng-repeat="x in names"> td>{{ x.Name }}</>{{ x.Country }}tr> > divscript> var app = angular.module('myApp,[]); app.controller(customersCtrlfunction($scope,$http) { $http.get("http://customers_mysql.php) .success( (response) {$scope.names response.records;}); }); > 通过ASP.NET Server从MSSQL数据库中获取数据 http://customers_sql.aspx
服务器代码示例下面几小节列出了几种不同的服务器端代码,用来从数据库中获取数据。 1. 使用PHP和MySQL。返回JSON数据。 2. 使用PHP和MS Access。返回JSON数据。 3. 使用ASP.NET,VB和MS Access。返回JSON数据。 4. 使用ASP.NET,Razor和SQL Lite。返回JSON数据。 跨站HTTP请求 从不同的服务器请求数据被称为跨站HTTP请求(即cross-siteHTTP requests)。 跨站HTTP请求在web开发中很普遍。许多页面常常需要从不同的服务器加载各种资源,如CSS,images和scripts等。 在现代浏览器中,出于安全考虑,通过脚本进行跨站HTTP请求被严格限制,只允许访问同一站点内的数据。 下面这行代码被用在PHP中,用来允许跨站HTTP请求。 header("Access-Control-Allow-Origin: *");
1. 使用PHP和MySQL <?php header("Access-Control-Allow-Origin: *"); header("Content-Type: application/json; charset=UTF-8"); $conn = new mysqli("myServer","myUser","myPassword","Northwind"); $result = $conn->query("SELECT CompanyName,City,Country FROM Customers"); $outp = ""; while($rs = $result->fetch_array(MYSQLI_ASSOC)) { if ($outp != "") {$outp .= ",";} $outp .= '{"Name":"' . $rs["CompanyName"] . '",'; $outp .= '"City":"' . $rs["City"] . '",128); line-height:1.5!important">$outp.= '"Country":"'. $rs["Country"] . '"}'; } $outp ='{"records":['.$outp.']}'; $conn->close(); echo($outp); ?> |