AngularJs之$http服务前端与后端之间的数据传输

AngularJs中的$http服务实现前端与后端之间的数据传输,与JQuery中的ajax类似,具体代码如下:

1、接收后端数据:

需要用到的php代码,文件名为1.php:

1<?php
2    header('Content-type:text/html;charset="utf-8"');
3 
4    $arry array(
5        array('webname'=>'赵一鸣个人技术博客''weburl'=>'http://www.zymseo.com'),
6        array('webname'=>'太原雅辉装修网''weburl'=>'http://www.0351zhuangxiu.com')
7    );
8 
9    echo json_encode($arry);

前端代码:

1var m = angular.module('app', []);
2m.controller('ctr2', ['$scope''$http'function($scope, $http){
3    $http({
4        'url' './1.php',
5        'method' 'post'
6    }).then(function(result){
7        console.log(result);
8    });
9}]);

打印结果如下:

1Object { data: Array[2], status: 200, headers: bd/<(), config: Object, statusText: "OK" }

最后解析对象的data属性,就可以得到我们需要的数据!

2、向后端发送数据:

前端代码:

01var m = angular.module('app', []);
02m.controller('ctr2', ['$scope''$http'function($scope, $http){
03    $http({
04        'url' './1.php',
05        'method' 'post',
06        'data' : {
07            'name' '赵一鸣',
08            'sex' '男',
09            'work' 'WEB前端开发'
10        }
11    }).then(function(result){
12        console.log(result);
13    });
14}]);

后端PHP代码:

1<?php
2    header('Content-type:text/html;charset="utf-8"');
3 
4    $content file_get_contents('php://input');
5     
6    print_r(json_decode($content, true));

注意:用PHP接受AnguarJs传输的数据时,需要用到file_get_contents这个方法,然后参数是'php://input',这样才能正常解析使用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值