This article mainly introduces $ http in Angularjs to pass parameters through the message body in post requests, and analyzes the settings and usage skills of using $ http to pass Parameters in post requests in combination with examples, for more information about how to transmit parameters through the message body in Angularjs, see the example below. We will share this with you for your reference. The details are as follows:
In Angularjs, $ http transmits parameters in the message body using post. You need to make the following changes to ensure the correctness of the parameters transmitted in the message body.
1. Set when declaring an application:
Var httpPost = function ($ httpProvider) {/************************************** * ***** note: $ When http post is submitted, correct the message body ************************************* * ***** // 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} ob J * @ return {String} */var param = function (obj) {var query = '', name, value, fullSubName, 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) + '=' + encodeURIComponent (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 );
Ii. Call $ http post
$ Http ({method: 'post', url: 'getdata. ashx', params: {id: '000000'}, // params as the url parameter data: {keyName: 'qubernet'} // as the message body parameter }, function (data ){});
For more information about how to implement $ http in Angularjs to transmit parameters through the message body in post requests, refer to PHP Chinese website!