angularJs中$http获取后台数据实例
来源:互联网 发布:mac地址有什么用 编辑:程序博客网 时间:2024/05/19 09:15
1.html
<div ng-app="module" ng-controller="ctrl"> <table border="1" width="600"> <tr> <td>网站名称</td> <td>网址</td> </tr> <tr ng-repeat="v in data"> <td>{{v.name}}</td> <td>{{v.url}}</td> </tr> </table></div><script> var m = angular.module('module', []); //注入http服务 m.controller('ctrl', ['$scope', '$http', function ($scope, $http) { $http({ method:'get', //get请求方式 url:'1.php' //请求地址 }).then(function(response){ //成功时执行 console.log(response); $scope.data = response.data; //得到请求的数据 },function(response){ //失败时执行 console.log(response); }); }]);</script>
1.php
<?php$data = [ [ 'name' => '百度', 'url' => 'www.baidu.com' ], [ 'name' => '腾讯', 'url' => 'www.qq.com' ],];echo json_encode($data,JSON_UNESCAPED_UNICODE);
上面是最简单的$http获取后台数据实例,假如一个页面中要异步加载一个后台文件好几次,那么是不是也要请求服务好几次呢?显然这样会使页面的加载出现迟钝,那么,我们可以通过缓存操作来减少服务器压力,使其尽快显示页面数据,那么,具体怎么做呢?很简单,在$http
中添加cache:true,
,即可解决,再刷新页面的时候,只会显示一次重复请求的数据。
$http({ method:'get', url:'1.php', cache:true, //避免多次请求后台数据 }).then(function(response){ //成功时执行 console.log(response); $scope.data = response.data; },function(response){ //失败时执行 console.log(response); });
当然,像jquery的ajax请求那样,angularjs也可以进行简写,
m.controller('ctrl', ['$scope', '$http', function ($scope, $http) { //post方式 //$http.post('1.php',{id:1})参数里可加属性 $http.post('1.php').then(function(response){ //成功时执行 console.log(response); $scope.data = response.data; }); }]);
m.controller('ctrl', ['$scope', '$http', function ($scope, $http) { //get方式 //$http.get('1.php',{cache:true}) 参数里可加属性 $http.get('1.php').then(function(response){ //成功时执行 console.log(response); $scope.data = response.data; }); }]);
最后,来说下 $http服务之后台接收POST数据的几种方式:
<div ng-app="module" ng-controller="ctrl"></div><script> var m = angular.module('module', []); m.controller('ctrl', ['$scope', '$http', function ($scope, $http) { //第一种方式/* $http({ method:'post', url:'1.php', data:{id:1,name:'后盾人'} }).then(function(response){ console.log(response.data); })*/ //第二种方式 $http({ method:'post', url:'1.php', data:$.param({id:1,name:'后盾人'}), headers:{'Content-type':'application/x-www-form-urlencoded'} }).then(function(response){ console.log(response.data); }) }]);</script>
<?php#第一种处理方法//$content = file_get_contents('php://input');//print_r(json_decode($content,true));#第二种方式print_r($_POST);
阅读全文
4 0
- angularJs中$http获取后台数据实例
- AngularJS $http 异步后台无法获取请求参数
- AngularJS $http 异步后台无法获取请求参数
- AngularJS $http 异步后台无法获取请求参数
- angularjs的http请求后台收不到数据
- 通过http协议获取后台数据
- 一.angularJs——服务调用与后台数据获取
- Angularjs $http获取json
- JAVA程序通过后台登陆网站,获取Session,然后再POST Http Request添加数据到数据库的实例
- C#angularjs读取后台数据
- js中获取后台的集合数据
- thinkphp 后台获取argularjs http提交的post数据
- java后台 通过HTTP接口获取json数据
- AngularJS 获取接口数据
- AngularJS 获取JSON数据
- 通过AngularJS实现前端与后台的数据对接——服务(service,$http)
- Autocomplete插件jQuery搜索提示框实例-后台获取数据
- 饼状图向下钻取:自己实例,ajax后台获取数据
- Android 中关于 【Cursor】 类的介绍
- TCP Split Handshake Attack
- java项目中想获取当前项目的路径
- 设计模式(一)----工厂模式
- jdk8并行流
- angularJs中$http获取后台数据实例
- OpenGL纹理设置
- Linux--shell知识点
- BlockChain
- Exception in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]" java.lang.OutOfMemoryEr
- TensorFlow的自编码网络实现(MNIST无监督学习)
- 毕业杂谈
- 函数模版、重载和特化的匹配规则
- 【故障】旧版本的Docker Toolbox 无法正常运行的解决办法