通过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
跨站HTTP请求
1. 使用PHP和MysqL
服务器代码示例
下面几小节列出了几种不同的服务器端代码,用来从数据库中获取数据。
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请求被严格限制,只允许访问同一站点内的数据。
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); ?>