The example in this article describes a method for passing parameters through a message body in Angularjs $http with a POST request. Share to everyone for your reference, specific as follows:
In Angularjs, $http passing parameters in the message body by post, you need to make the following modifications to ensure that the message body passes the parameters correctly.
First, when the declaration of application to set:
var httppost = function ($httpProvider) {/******************************************* Description: Correcting message body when post submission $http ///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, Fullsubna
Me, 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) + ' = ' + encode
Uricomponent (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;
}
];
};
var Ngapp = angular.module (' Wtapp ', [' ngcookies '], httppost);
Second, call $http post
$http ({method
: ' POST ',
URL: ' getdata.ashx ',
params: {id: ' 1002 '},//params as a URL parameter
data: {keyname: ' Qubernet '}//as the message body parameter
}, function (data) {
});
I hope this article will help you to Angularjs program design.