AngularJS入门教程中SQL实例详解
程序员文章站
2022-03-20 21:24:24
angularjs sql
在前面章节中的代码也可以用于读取数据库中的数据。
使用 php 从 mysql 中获取数据
angularjs 实例...
angularjs sql
在前面章节中的代码也可以用于读取数据库中的数据。
使用 php 从 mysql 中获取数据
angularjs 实例
<!doctype html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <style> table, th , td { border: 1px solid grey; border-collapse: collapse; padding: 5px; } table tr:nth-child(odd) { background-color: #f1f1f1; } table tr:nth-child(even) { background-color: #ffffff; } </style> </head> <body> <div ng-app="myapp" ng-controller="customersctrl"> <table> <tr ng-repeat="x in names"> <td>{{ x.name }}</td> <td>{{ x.country }}</td> </tr> </table> </div> <script> var app = angular.module('myapp', []); app.controller('customersctrl', function($scope, $http) { $http.get("/try/angularjs/data/customers_mysql.php") .success(function (response) {$scope.names = response.records;}); }); </script> </body> </html>
运行结果:
alfreds futterkiste | germany |
ana trujillo emparedados y helados | mexico |
antonio moreno taquería | mexico |
around the horn | uk |
b's beverages | uk |
berglunds snabbköp | sweden |
blauer see delikatessen | germany |
blondel père et fils | france |
bólido comidas preparadas | spain |
bon app' | france |
bottom-dollar marketse | canada |
cactus comidas para llevar | argentina |
centro comercial moctezuma | mexico |
chop-suey chinese | switzerland |
comércio mineiro | brazil |
asp.net 中执行 sql 获取数据
angularjs 实例
<!doctype html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <style> table, th , td { border: 1px solid grey; border-collapse: collapse; padding: 5px; } table tr:nth-child(odd) { background-color: #f1f1f1; } table tr:nth-child(even) { background-color: #ffffff; } </style> </head> <body> <div ng-app="myapp" ng-controller="customersctrl"> <table> <tr ng-repeat="x in names"> <td>{{ x.name }}</td> <td>{{ x.country }}</td> </tr> </table> </div> <script> var app = angular.module('myapp', []); app.controller('customersctrl', function($scope, $http) { $http.get("http://www.runoob.com/try/angularjs/data/customers_sql.aspx") .success(function (response) {$scope.names = response.records;}); }); </script> </body> </html>
运行结果:
alfreds futterkiste | germany |
berglunds snabbköp | sweden |
centro comercial moctezuma | mexico |
ernst handel | austria |
fissa fabrica inter. salchichas s.a. | spain |
galería del gastrónomo | spain |
island trading | uk |
königlich essen | germany |
laughing bacchus wine cellars | canada |
magazzini alimentari riuniti | italy |
north/south | uk |
paris spécialités | france |
rattlesnake canyon grocery | usa |
simons bistro | denmark |
the big cheese | usa |
vaffeljernet | denmark |
wolski zajazd | poland |
服务端代码
以下列出了列出了几种服务端代码类型:
使用 php 和 mysql。返回 json。
使用 php 和 ms access。返回 json。
使用 asp.net, vb, 及 ms access。 返回 json。
使用 asp.net, razor, 及 sql lite。 返回 json。
跨域 http 请求
如果你需要从不同的服务器(不同域名)上获取数据就需要使用跨域 http 请求。
跨域请求在网页上非常常见。很多网页从不同服务器上载入 css, 图片,js脚本等。
在现代浏览器中,为了数据的安全,所有请求被严格限制在同一域名下,如果需要调用不同站点的数据,需要通过跨域来解决。
以下的 php 代码运行使用的网站进行跨域访问。
header("access-control-allow-origin: *");
更多跨域访问解决方案可参阅:php ajax 跨域问题最佳解决方案。
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"] . '",'; $outp .= '"country":"'. $rs["country"] . '"}'; } $outp ='{"records":['.$outp.']}'; $conn->close(); echo($outp); ?>
2. php 和 ms access 代码实例
<?php header("access-control-allow-origin: *"); header("content-type: application/json; charset=iso-8859-1"); $conn = new com("adodb.connection"); $conn->open("provider=microsoft.jet.oledb.4.0;data source=northwind.mdb"); $rs = $conn->execute("select companyname, city, country from customers"); $outp = ""; while (!$rs->eof) { if ($outp != "") {$outp .= ",";} $outp .= '{"name":"' . $rs["companyname"] . '",'; $outp .= '"city":"' . $rs["city"] . '",'; $outp .= '"country":"'. $rs["country"] . '"}'; $rs->movenext(); } $outp ='{"records":['.$outp.']}'; $conn->close(); echo ($outp); ?>
3. asp.net, vb 和 ms access 代码实例
<%@ import namespace="system.io"%> <%@ import namespace="system.data"%> <%@ import namespace="system.data.oledb"%> <% response.appendheader("access-control-allow-origin", "*") response.appendheader("content-type", "application/json") dim conn as oledbconnection dim objadapter as oledbdataadapter dim objtable as datatable dim objrow as datarow dim objdataset as new dataset() dim outp dim c conn = new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=northwind.mdb") objadapter = new oledbdataadapter("select companyname, city, country from customers", conn) objadapter.fill(objdataset, "mytable") objtable=objdataset.tables("mytable") outp = "" c = chr(34) for each x in objtable.rows if outp <> "" then outp = outp & "," outp = outp & "{" & c & "name" & c & ":" & c & x("companyname") & c & "," outp = outp & c & "city" & c & ":" & c & x("city") & c & "," outp = outp & c & "country" & c & ":" & c & x("country") & c & "}" next outp ="{" & c & "records" & c & ":[" & outp & "]}" response.write(outp) conn.close %>
4. asp.net, vb razor 和 sql lite 代码实例
@{ response.appendheader("access-control-allow-origin", "*") response.appendheader("content-type", "application/json") var db = database.open("northwind"); var query = db.query("select companyname, city, country from customers"); var outp ="" var c = chr(34) } @foreach(var row in query) { if outp <> "" then outp = outp + "," outp = outp + "{" + c + "name" + c + ":" + c + @row.companyname + c + "," outp = outp + c + "city" + c + ":" + c + @row.city + c + "," outp = outp + c + "country" + c + ":" + c + @row.country + c + "}" } outp ="{" + c + "records" + c + ":[" + outp + "]}" @outp
以上就是angularjs sql资料的整理,后续继续补充,希望能帮助学习的朋友。
上一篇: 苹果Mac系统关机很慢该怎么解决?