使用 transformRequest 将 JSON 转换为 FormURL 编码数据
Convert JSON as FormURL Encoded data using transformRequest
我正在尝试将 JSON 数据转换为 formURL 编码数据,但仍然无法正常工作。
我的 HTTP post
$http.post(API_ENDPOINT.login, credentials, {
transformRequest: transformRequestAsFormPost
})
我的转换请求
'use strict';
define(['app-module'], function(app) {
$app.info('transformRequest initialized');
return app.factory('transformRequestAsFormPost', function() {
function transformRequest(data, getHeaders) {
var headers = getHeaders();
headers["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8";
return (serializeData(data));
}
function serializeData(data) {
if (!angular.isObject(data)) {
return ((data === null) ? "" : data.toString());
}
var buffer = [];
for (var name in data) {
if (!data.hasOwnProperty(name)) {
continue;
}
var value = data[name];
buffer.push(
encodeURIComponent(name) +
"=" +
encodeURIComponent((value == null) ? "" : value)
);
console.log(buffer)
}
var source = buffer.join("&").replace(/%40/g, "@");
return (source);
}
return (transformRequest);
});
});
我不知道我做错了什么。当我将任何 JSON 对象传递给它时,它 returns 一个字符串。
由于5da1256, transformRequest
functions can no longer modify request headers. This behavior was unintended and undocumented, so the change should affect very few applications.1
发送 POST 请求 Content-Type: application/x-www-form-urlencoded
:
var config = {
transformRequest: $httpParamSerializer,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
$http.post(API_ENDPOINT.login, credentials, config)
.then(function(response) {
console.log("SUCCESS");
}).catch(function(response) {
console.log("ERROR");
throw response;
});
我正在尝试将 JSON 数据转换为 formURL 编码数据,但仍然无法正常工作。
我的 HTTP post
$http.post(API_ENDPOINT.login, credentials, {
transformRequest: transformRequestAsFormPost
})
我的转换请求
'use strict';
define(['app-module'], function(app) {
$app.info('transformRequest initialized');
return app.factory('transformRequestAsFormPost', function() {
function transformRequest(data, getHeaders) {
var headers = getHeaders();
headers["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8";
return (serializeData(data));
}
function serializeData(data) {
if (!angular.isObject(data)) {
return ((data === null) ? "" : data.toString());
}
var buffer = [];
for (var name in data) {
if (!data.hasOwnProperty(name)) {
continue;
}
var value = data[name];
buffer.push(
encodeURIComponent(name) +
"=" +
encodeURIComponent((value == null) ? "" : value)
);
console.log(buffer)
}
var source = buffer.join("&").replace(/%40/g, "@");
return (source);
}
return (transformRequest);
});
});
我不知道我做错了什么。当我将任何 JSON 对象传递给它时,它 returns 一个字符串。
由于5da1256, transformRequest
functions can no longer modify request headers. This behavior was unintended and undocumented, so the change should affect very few applications.1
发送 POST 请求 Content-Type: application/x-www-form-urlencoded
:
var config = {
transformRequest: $httpParamSerializer,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
$http.post(API_ENDPOINT.login, credentials, config)
.then(function(response) {
console.log("SUCCESS");
}).catch(function(response) {
console.log("ERROR");
throw response;
});