AngularJS SQL

导语 AngularJS SQL使用 PHP 从 MySQL 中获取数据AngularJS 实例<divng-app="myApp"ng-controller="customersCtrl"><table><trng-repeat="xinnames"><td>{{x Name}}< td><td>{{x Country}}< td>< tr>< table><
AngularJS SQL
使用 PHP 从 MySQL 中获取数据
AngularJS 实例
  1. <div ng-app="myApp" ng-controller="customersCtrl">  
  2.  
  3. <table>
  4.   <tr ng-repeat="x in names">
  5.     <td>{{ x.Name }}</td>
  6.     <td>{{ x.Country }}</td>
  7.   </tr>
  8. </table>
  9.  
  10. </div>
  11.  
  12. <script>
  13. var app = angular.module('myApp', []); 
  14. app.controller('customersCtrl', function($scope, $http) { 
  15.     $http.get("/angularjs/data/Customers_MySQL.php") 
  16.     .success(function (response) {$scope.names = response.records;}); 
  17. }); 
  18. </script>
服务端代码
使用 PHP 和 MySQL。返回 JSON。
 
跨域 HTTP 请求
如果你需要从不同的服务器(不同域名)上获取数据就需要使用跨域 HTTP 请求。
跨域请求在网页上非常常见。很多网页从不同服务器上载入 CSS, 图片,Js脚本等。
在现代浏览器中,为了数据的安全,所有请求被严格限制在同一域名下,如果需要调用不同站点的数据,需要通过跨域来解决。
以下的 PHP 代码运行使用的网站进行跨域访问。
header("Access-Control-Allow-Origin: *");
更多跨域访问解决方案可参阅:PHP Ajax 跨域问题最佳解决方案。
1. PHP 和 MySql 代码实例
  1. <?php 
  2. header("Access-Control-Allow-Origin: *"); 
  3. header("Content-Type: application/json; charset=UTF-8"); 
  4.  
  5. $conn = new mysqli("myServer""myUser""myPassword""Northwind"); 
  6.  
  7. $result = $conn->query("SELECT CompanyName, City, Country FROM Customers"); 
  8.  
  9. $outp = ""
  10. while($rs = $result->fetch_array(MYSQLI_ASSOC)) { 
  11.     if ($outp != "") {$outp .= ",";} 
  12.     $outp .= '{"Name":"'  . $rs["CompanyName"] . '",'; 
  13.     $outp .= '"City":"'   . $rs["City"]        . '",'; 
  14.     $outp .= '"Country":"'$rs["Country"]     . '"}';  
  15. $outp ='{"records":['.$outp.']}'
  16. $conn->close(); 
  17.  
  18. echo($outp); 
  19. ?> 

https://www.nucmc.com/ true AngularJS SQL https://www.nucmc.com/show-69-790-1.html report 3193 AngularJS SQL使用 PHP 从 MySQL 中获取数据AngularJS 实例<divng-app="myApp"ng-controller="customersCtrl"><table><trng-repeat="xinnames"><td>{{x Name}}< td><td>{{x Country}}< td>< tr>< table><
TAG:AngularJS SQL
本站欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明: 文章转载自:BETWAY官网网 https://www.nucmc.com/show-69-790-1.html
BETWAY官网网 Copyright 2012-2014 www.nucmc.com All rights reserved.(晋ICP备13001436号-1)