我有一个表单,在提交时,数据作为请求有效负载而不是表单数据发送。
define(['app-module'], function(app) {
'use strict';
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)
);
}
var source = buffer.join("&").replace(/%20/g, "+");
return (source);
}
return (transformRequest);
});
});