How to Implement $ http in Angularjs to transmit parameters through the message body using a post request

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.