首页 >> js开发 >> jsangular中的post请求处理示例详解js大全
jsangular中的post请求处理示例详解js大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
前言前言前言项目angular中使用jQuery请求,想替换为angular自身请求,结果发现后台没法获取參数,所以,查询资料分析一下,做个总结。步骤如下:步骤如下:步骤如下:首先,angular和jQuery中请求是不同的。如下:
jQuery:
jQuery:
请求contentType是:
application/x-www-form-urlencoded; charset=UTF-8
application/x-www-form-urlencoded; charset=UTF-8该类型数据被编码成以 '&' 分隔的键-值对, 同时以 '=' 分隔键和值. 非字母或数字的字符会被百分比编码: 这也就是为什么这种类型不支持二进制数据(应使用 multipart/form-data 代替)。data参数是处理过的:
// json对象
{ a : 3, b : 2 }
// 将json对象处理为
"a=3&b=2"
// json对象
{ a : 3, b : 2 }
// 将json对象处理为
"a=3&b=2"
Angular:
Angular:
请求contentType:
application/json
application/jsondata参数:
// json对象
{a: 3,c: 2}
// json对象
{a: 3,c: 2}综上来看,angular提交后台是json,不是表单数据。我们需要把json对象转换为参数拼接,提交后台时就是表单数据了:
/**
* 将application/json转换为application/x-www-form-urlencoded
* @param data
*/
handlerPostParams(data) {
const params = [];
for (const key in data) {
if (data[key] && !isNull(data[key])) {
if (data[key] instanceof Array) {
for (let i = 0; i < data[key].length; i++) {
if (data[key][i] && !isNull(data[key][i])) {
const value = data[key][i];
const name = key + '[' + i + ']';
const innerObj = {};
innerObj[name] = value;
params.push(this.handlerPostParams(innerObj));
}
}
} else if (data[key] instanceof Object) {
for (const k in data[key]) {
if (data[key][k] && !isNull(data[key][k])) {
const value = data[key][k];
const name = key + '[' + k + ']';
const innerObj = {};
innerObj[name] = value;
params.push(this.handlerPostParams(innerObj));
}
}
} else {
const param = encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
params.push(param);
}
}
}
return params.join('&');
}
/**
* 将application/json转换为application/x-www-form-urlencoded
* @param data
*/
handlerPostParams(data) {
const params = [];
for (const key in data) {
if (data[key] && !isNull(data[key])) {
if (data[key] instanceof Array) {
for (let i = 0; i < data[key].length; i++) {
if (data[key][i] && !isNull(data[key][i])) {
const value = data[key][i];
const name = key + '[' + i + ']';
const innerObj = {};
innerObj[name] = value;
params.push(this.handlerPostParams(innerObj));
}
}
} else if (data[key] instanceof Object) {
for (const k in data[key]) {
if (data[key][k] && !isNull(data[key][k])) {
const value = data[key][k];
const name = key + '[' + k + ']';
const innerObj = {};
innerObj[name] = value;
params.push(this.handlerPostParams(innerObj));
}
}
} else {
const param = encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
params.push(param);
}
}
}
return params.join('&');
}总结总结总结
jQuery:
jQuery:
请求contentType是:
application/x-www-form-urlencoded; charset=UTF-8
application/x-www-form-urlencoded; charset=UTF-8该类型数据被编码成以 '&' 分隔的键-值对, 同时以 '=' 分隔键和值. 非字母或数字的字符会被百分比编码: 这也就是为什么这种类型不支持二进制数据(应使用 multipart/form-data 代替)。data参数是处理过的:
// json对象
{ a : 3, b : 2 }
// 将json对象处理为
"a=3&b=2"
// json对象
{ a : 3, b : 2 }
// 将json对象处理为
"a=3&b=2"
Angular:
Angular:
请求contentType:
application/json
application/jsondata参数:
// json对象
{a: 3,c: 2}
// json对象
{a: 3,c: 2}综上来看,angular提交后台是json,不是表单数据。我们需要把json对象转换为参数拼接,提交后台时就是表单数据了:
/**
* 将application/json转换为application/x-www-form-urlencoded
* @param data
*/
handlerPostParams(data) {
const params = [];
for (const key in data) {
if (data[key] && !isNull(data[key])) {
if (data[key] instanceof Array) {
for (let i = 0; i < data[key].length; i++) {
if (data[key][i] && !isNull(data[key][i])) {
const value = data[key][i];
const name = key + '[' + i + ']';
const innerObj = {};
innerObj[name] = value;
params.push(this.handlerPostParams(innerObj));
}
}
} else if (data[key] instanceof Object) {
for (const k in data[key]) {
if (data[key][k] && !isNull(data[key][k])) {
const value = data[key][k];
const name = key + '[' + k + ']';
const innerObj = {};
innerObj[name] = value;
params.push(this.handlerPostParams(innerObj));
}
}
} else {
const param = encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
params.push(param);
}
}
}
return params.join('&');
}
/**
* 将application/json转换为application/x-www-form-urlencoded
* @param data
*/
handlerPostParams(data) {
const params = [];
for (const key in data) {
if (data[key] && !isNull(data[key])) {
if (data[key] instanceof Array) {
for (let i = 0; i < data[key].length; i++) {
if (data[key][i] && !isNull(data[key][i])) {
const value = data[key][i];
const name = key + '[' + i + ']';
const innerObj = {};
innerObj[name] = value;
params.push(this.handlerPostParams(innerObj));
}
}
} else if (data[key] instanceof Object) {
for (const k in data[key]) {
if (data[key][k] && !isNull(data[key][k])) {
const value = data[key][k];
const name = key + '[' + k + ']';
const innerObj = {};
innerObj[name] = value;
params.push(this.handlerPostParams(innerObj));
}
}
} else {
const param = encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
params.push(param);
}
}
}
return params.join('&');
}总结总结总结