Angular post-transfer background PHP cannot receive

Source: Internet
Author: User
Tags php server

Most of the time we need to submit post data in Ajax, similar to ANGULARJS and the encapsulated post.

But the post of jquery is obviously simpler and more humane than the angularjs.

The two look no different, the data submitted with Angularjs $http, on the PHP server can not be obtained through $_request/$_post.

This is because the post of the two handles the header differently ... jquery serializes the MyData as a JSON object, and angular does not.

Solution:

Modify the default handling of angular $httpprovider (the 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}*/   varparam =function(obj) {varquery = ', name, value, Fullsubname, SubName, Subvalue, innerobj, I;  for(Nameinchobj) {Value=Obj[name]; if(ValueinstanceofArray) {         for(i=0; i<value.length; + +)i) {subvalue=Value[i]; Fullsubname= name + ' [' + i + '] '; Innerobj= {}; Innerobj[fullsubname]=Subvalue; Query+ = param (innerobj) + ' & '; }      }      Else if(ValueinstanceofObject) {         for(SubNameinchvalue) {Subvalue=Value[subname]; Fullsubname= name + ' [' + SubName + '] '; Innerobj= {}; Innerobj[fullsubname]=Subvalue; Query+ = param (innerobj) + ' & '; }      }      Else if(Value!== undefined && value!==NULL) Query+ = encodeURIComponent (name) + ' = ' + encodeuricomponent (value) + ' & '; }          returnQuery.length? Query.substr (0, Query.length-1): query;   }; //Override $http Service ' s default transformrequest$httpProvider. defaults.transformrequest = [function(data) {returnAngular.isobject (data) && String (data)!== ' [Object File] '?param (data): data; }];});

Angular post-transfer background PHP cannot receive

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.