The implementation method of $http transfer parameters through message body by POST request in Angularjs _angularjs

Source: Internet
Author: User
Tags http post

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.

Related Article

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.