修改angular post提交json数据的格式_ $httpProvider配置
2016-08-04 18:20
555 查看
修改angular post提交默认格式是json数据的格式,我们如果想用$_Post接收数据的话,那么就得配置$httpProvider
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
.config(['$httpProvider',
function ($httpProvider) {
// Use x-www-form-urlencoded Content-Type
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
// Override $http service's default transformRequest
$httpProvider.defaults.transformRequest = [function(data)
{
/**
* The workhorse; converts an object to x-www-form-urlencoded serialization.
* @param {Object} obj
* @return {String}
*/
var param = function(obj)
{
var query = '';
var name, value, fullSubName, subName, subValue, innerObj, i;
console.log(obj);
for(name in obj)
{
value = obj[name];
console.log(value);
if(value instanceof Array)
{
console.log("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)
{
console.log("object");
for(subName in value)
{
subValue = value[subName];
if(subValue != null){
// fullSubName = name + '[' + subName + ']';
//user.userName = hmm & user.userPassword = 111
fullSubName = name + '.' + subName;
// fullSubName = subName;
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
}
}
else if(value !== undefined ) //&& value !== null
{
console.log("undefined");
query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}
}
return query.length ? query.substr(0, query.length - 1) : query;
};
return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
}]
$httpProvider.defaults.useXDomain = true;
// delete $httpProvider.defaults.headers.common['X-Requested-With'];
}])
复制代码
参考:http://blog.sina.com.cn/s/blog_5be1dc830101k2ds.html
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
.config(['$httpProvider',
function ($httpProvider) {
// Use x-www-form-urlencoded Content-Type
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
// Override $http service's default transformRequest
$httpProvider.defaults.transformRequest = [function(data)
{
/**
* The workhorse; converts an object to x-www-form-urlencoded serialization.
* @param {Object} obj
* @return {String}
*/
var param = function(obj)
{
var query = '';
var name, value, fullSubName, subName, subValue, innerObj, i;
console.log(obj);
for(name in obj)
{
value = obj[name];
console.log(value);
if(value instanceof Array)
{
console.log("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)
{
console.log("object");
for(subName in value)
{
subValue = value[subName];
if(subValue != null){
// fullSubName = name + '[' + subName + ']';
//user.userName = hmm & user.userPassword = 111
fullSubName = name + '.' + subName;
// fullSubName = subName;
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
}
}
else if(value !== undefined ) //&& value !== null
{
console.log("undefined");
query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}
}
return query.length ? query.substr(0, query.length - 1) : query;
};
return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
}]
$httpProvider.defaults.useXDomain = true;
// delete $httpProvider.defaults.headers.common['X-Requested-With'];
}])
复制代码
参考:http://blog.sina.com.cn/s/blog_5be1dc830101k2ds.html
相关文章推荐
- extjs Post方法提交json格式的数据到php
- Http Post请求提交json格式数据工具类,解决请求参数中文乱码问题
- 向Solr数据集提交Json格式数据(Scala,Post)
- 以$.post方式提交数据 并以json格式返回数据
- 客户端POST提交、服务器返回json格式数据的demo
- extjs Post方法提交json格式的数据到php
- AJAX如何以POST的方式提交JSON数据到Form
- Get和Post区别,EncType提交数据的格式详解
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及文件上传的部分
- java(jsp/servlet )取得客户端post过来的json格式数据(jquery等)
- 客户端封装数据为json格式的数据提交到服务端的方法
- Linux终端使用curl来post数据,数据格式为json
- 使用jquery和html5 post json格式数据
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及非文件上传的部分
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- PHP中利用jQuery操作json格式数据,实现$_POST的数据传输和接收
- Fiddler进行模拟Post提交json数据,总为null解决方式
- Restful POST请求提交json或www-form-urlencoded数据格式的选择
- Mvc ModelBinder 一对多自定义数据格式 post提交
- 【转】jQuery使用 $.post提交json数据