当前位置:Gxlcms > 数据库问题 > 【10】AngularJS SQL

【10】AngularJS SQL

时间:2021-07-01 10:21:17 帮助过:2人阅读

SQL

 

使用 PHP 从 MySQL 中获取数据

  1. <div ng-app="myApp" ng-controller="customersCtrl">
  2. <table>
  3. <tr ng-repeat="x in names">
  4. <td>{{ x.Name}}</td>
  5. <td>{{ x.Country}}</td>
  6. </tr>
  7. </table>
  8. </div>
  9. <script>
  10. var app = angular.module(‘myApp‘,[]);
  11. app.controller(‘customersCtrl‘,function($scope, $http){
  12. $http.get("test.php")
  13. .success(function(response){$scope.names = response.records;});
  14. });
  15. </script>
 

ASP.NET 中执行 SQL 获取数据

  1. <div ng-app="myApp" ng-controller="customersCtrl">
  2. <table>
  3. <tr ng-repeat="x in names">
  4. <td>{{ x.Name}}</td>
  5. <td>{{ x.Country}}</td>
  6. </tr>
  7. </table>
  8. </div>
  9. <script>
  10. var app = angular.module(‘myApp‘,[]);
  11. app.controller(‘customersCtrl‘,function($scope, $http){
  12. $http.get("test.aspx")
  13. .success(function(response){$scope.names = response.records;});
  14. });
  15. </script>
  test.aspx 内容:  
  1. {"records":[
  2. {
  3. "Name":"Alfreds Futterkiste",
  4. "City":"Berlin",
  5. "Country":"Germany"
  6. },
  7. {
  8. "Name":"Berglunds snabbköp",
  9. "City":"Luleå",
  10. "Country":"Sweden"
  11. },
  12. {
  13. "Name":"Centro comercial Moctezuma",
  14. "City":"México D.F.",
  15. "Country":"Mexico"
  16. },
  17. {
  18. "Name":"Ernst Handel",
  19. "City":"Graz",
  20. "Country":"Austria"
  21. },
  22. {
  23. "Name":"FISSA Fabrica Inter. Salchichas S.A.",
  24. "City":"Madrid",
  25. "Country":"Spain"
  26. },
  27. {
  28. "Name":"Galería del gastrónomo",
  29. "City":"Barcelona",
  30. "Country":"Spain"
  31. },
  32. {
  33. "Name":"Island Trading",
  34. "City":"Cowes",
  35. "Country":"UK"
  36. },
  37. {
  38. "Name":"Königlich Essen",
  39. "City":"Brandenburg",
  40. "Country":"Germany"
  41. },
  42. {
  43. "Name":"Laughing Bacchus Wine Cellars",
  44. "City":"Vancouver",
  45. "Country":"Canada"
  46. },
  47. {
  48. "Name":"Magazzini Alimentari Riuniti",
  49. "City":"Bergamo",
  50. "Country":"Italy"
  51. },
  52. {
  53. "Name":"North/South",
  54. "City":"London",
  55. "Country":"UK"
  56. },
  57. {
  58. "Name":"Paris spécialités",
  59. "City":"Paris",
  60. "Country":"France"
  61. },
  62. {
  63. "Name":"Rattlesnake Canyon Grocery",
  64. "City":"Albuquerque",
  65. "Country":"USA"
  66. },
  67. {
  68. "Name":"Simons bistro",
  69. "City":"København",
  70. "Country":"Denmark"
  71. },
  72. {
  73. "Name":"The Big Cheese",
  74. "City":"Portland",
  75. "Country":"USA"
  76. },
  77. {
  78. "Name":"Vaffeljernet",
  79. "City":"Århus",
  80. "Country":"Denmark"
  81. },
  82. {
  83. "Name":"Wolski Zajazd",
  84. "City":"Warszawa",
  85. "Country":"Poland"
  86. }
  87. ]}
 

服务端代码

以下列出了列出了几种服务端代码类型:

  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 请求。

跨域请求在网页上非常常见。很多网页从不同服务器上载入 CSS, 图片,Js脚本等。

在现代浏览器中,为了数据的安全,所有请求被严格限制在同一域名下,如果需要调用不同站点的数据,需要通过跨域来解决。

以下的 PHP 代码运行使用的网站进行跨域访问。

 

  1. header("Access-Control-Allow-Origin: *");
 

 

 

 


1. PHP 和 MySql 代码实例

  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. header("Content-Type: application/json; charset=UTF-8");
  4. $conn =new mysqli("myServer","myUser","myPassword","Northwind");
  5. $result = $conn->query("SELECT CompanyName, City, Country FROM Customers");
  6. $outp ="";
  7. while($rs = $result->fetch_array(MYSQLI_ASSOC)){
  8. if($outp !=""){$outp .=",";}
  9. $outp .=‘{"Name":"‘. $rs["CompanyName"].‘",‘;
  10. $outp .=‘"City":"‘. $rs["City"].‘",‘;
  11. $outp .=‘"Country":"‘. $rs["Country"].‘"}‘;
  12. }
  13. $outp =‘{"records":[‘.$outp.‘]}‘;
  14. $conn->close();
  15. echo($outp);
  16. ?>
 

2. PHP 和 MS Access 代码实例

  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. header("Content-Type: application/json; charset=ISO-8859-1");
  4. $conn =new COM("ADODB.Connection");
  5. $conn->open("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb");
  6. $rs = $conn->execute("SELECT CompanyName, City, Country FROM Customers");
  7. $outp ="";
  8. while(!$rs->EOF){
  9. if($outp !=""){$outp .=",";}
  10. $outp .=‘{"Name":"‘. $rs["CompanyName"].‘",‘;
  11. $outp .=‘"City":"‘. $rs["City"].‘",‘;
  12. $outp .=‘"Country":"‘. $rs["Country"].‘"}‘;
  13. $rs->MoveNext();
  14. }
  15. $outp =‘{"records":[‘.$outp.‘]}‘;
  16. $conn->close();
  17. echo ($outp);
  18. ?>
 

3. ASP.NET, VB 和 MS Access 代码实例

  1. <%@ImportNamespace="System.IO"%>
  2. <%@ImportNamespace="System.Data"%>
  3. <%@ImportNamespace="System.Data.OleDb"%>

人气教程排行