Angular post parameter passing backend php cannot receive, angularpost

Source: Internet
Author: User

Angular post parameter passing backend php cannot receive, angularpost

In many cases, we need to use ajax to submit post data. angularjs is similar to jq and has encapsulated post.

However, jQuery post is much simpler and more user-friendly than angularjs post.

There seems to be no difference between the two. The data submitted with $ http from angularjs cannot be obtained through $ _ REQUEST/$ _ POST on the php server.

This is because the post processing of the header is different between the two ...... JQuery serializes myData as a JSON object,Angular does not.

Solution:

Modify the default processing of Angular $ httpProvider (the most perfect solution)

angular.module('MyModule', [], function($httpProvider) {  // 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, 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;  }];});

 

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.