AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
来源:互联网 发布:win10哪些软件无用 编辑:程序博客网 时间:2024/06/05 08:51
最近在使用AngularJs+Php开发中遇到php后台无法接收到来自AngularJs的数据,在网上也有许多解决方法,却都点到即止.多番摸索后记录下解决方法:
tips:当前使用的AngularJs版本为v1.5.0-rc.0
tips:当前使用的AngularJs版本为v1.5.0-rc.0
原因分析:
在使用jquery的时候进行post请求的时候很简单.
$.ajax({ type: 'POST', url:'process.php', data: formData, dataType: 'json', success: function(result){ //do something } });
对这个传输的数据我们一般会直接使用serialize()或使用serializeArray()处理后再传输,但在发送post请求时jquery会把这个对象转换为字符串后再发送,类似"a=123&b=456".
而AngularJs传输的是一个Json数据而不是一个转换后的字符串,在php端接收的时候不能直接使用$_POST方式接收.这样是获取不到数据的.
$POST方式只能接收Content-Type: application/x-www-form-urlencoded提交的数据,也就是表单提交的数据.
但可以使用file_get_contents("php://input")接收,对于没有没有指定Content-Type的Post数据也是可以接收到的,此时获取到的是个字符串还需要再转换才能变成我们想要的数据格式.这样无疑增加了工作量.
解决方案:
1.引用JQuery,使用JQuery的$.param()方法序列化参数后传递
$http({ method : 'POST', url: 'process.php', data: $.param($scope.formData), //序列化参数 headers: { 'Content-Type': 'application/x-www-form-urlencoded' } ) })
2.使用file_get_contents("php://input")获取再处理
$input = file_get_contents("php://input",true);echo $input;
3.修改Angular的$httpProvider的默认处理(参考:http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/)
// Your app's root module...angular.module('MyModule', [], function($httpProvider) { // Use x-www-form-urlencoded Content-Type $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; /** * The workhorse; converts an object to x-www-form-urlencoded serialization. * @param {Object} obj * @return {String} */ var param = function(obj) { var query = '', name, value, fullSubName, subName, subValue, innerObj, i; for(name in obj) { value = obj[name]; if(value instanceof Array) { for(i=0; i<value.length; ++i) { subValue = value[i]; fullSubName = name + '[' + i + ']'; innerObj = {}; innerObj[fullSubName] = subValue; query += param(innerObj) + '&'; } } else if(value instanceof Object) { for(subName in value) { subValue = value[subName]; fullSubName = name + '[' + subName + ']'; innerObj = {}; innerObj[fullSubName] = subValue; query += param(innerObj) + '&'; } } else if(value !== undefined && value !== null) query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&'; } return query.length ? query.substr(0, query.length - 1) : query; }; // Override $http service's default transformRequest $httpProvider.defaults.transformRequest = [function(data) { return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data; }];});
$http({ method:"POST", url:"/api/login.php", data:$scope.Account });
补:
php获取时也可通过$GLOBALS
[
'HTTP_RAW_POST_DATA'
]获取POST提交的原始数据.
但$GLOBALS['HTTP_RAW_POST_DATA']中是否保存POST过来的数据取决于centent-Type的设置,即POST数据时 必须显式示指明Content-Type: application/x-www-form-urlencoded,POST的数据才会存放到 $GLOBALS['HTTP_RAW_POST_DATA']中.
0 0
- AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
- PHP后端接收不到AngularJs中$http.post发送的数据的问题
- AngularJs中$http发送post或者get请求,SpringMVC后台接收不到参数值的解决办法
- 解决 AngularJs $http 发送post,get请求,SpringMvc无法接受参数的问题。
- 解决PHP无法通过$_POST接收到AngularJS的post数据的解决方法
- 发送http的post请求
- angularJS里的$http.post传数据到php接收问题
- 发送http的post请求并通过request接收post请求体
- HttpConnection的post请求发送数据,接收方出现乱码
- weiphp2.0 controller.class.php里面add方法接收不到post请求发送的数据
- PHP中接收send/POST/GET发送的数据
- php 发送http post请求
- php http发送get、post请求的几种方法
- php 无法接收post数据
- java 发送http协议的POST请求获取数据
- Node.js中http 接收get请求、接收post请求、发送get请求、发送post请求
- python通过get,post方式发送http请求和接收http响应的方法
- python通过get,post方式发送http请求和接收http响应的方法
- QTP 利用DOM实现拖动浏览器滚动条
- Android网络加载时显示进度条对话框
- win8.1右键新建菜单添加新建php文件
- [Asp.net mvc]实体更新异常:存储区更新、插入或删除语句影响到了意外的行数(0)。实体在加载后可能被修改或删除。
- [Asp.net mvc] 在Asp.net mvc 中使用MiniProfiler
- AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
- Visual Studio Code 添加设置代码段(snippet)
- js数组与字符串的相互转化
- C#6.0新特性的尝试
- VS2015 安装mvc4安装包以及vs2010 sp1后导致Razor语法失效代码不高亮(能正常运行)/视图页面无法智能提示(.cshtml)解决办法
- Asp.net Mvc 使用EF6 code first 方式连接MySQL总结
- Mysql 5.7.12解压版的安装及配置系统编码
- 让Chrome支持Ajax/$http方式读取本地文件
- Android中构建HTML5应用