Angular.module (' myApp ', []). config (function ($httpProvider) {$httpProvider. Defaults.usexdomain = true; Delete $httpProvider. defaults.headers. common[' X-requested-with '; $httpProvider. defaults.headers.post[' content-type '] = ' application/x-www-form-urlencoded;charset=utf-8 '; $httpProvider. defaults.headers.put[' content-type '] = ' application/x-www-form-urlencoded;charset=utf-8 '; Override $http Service ' s default transformrequest $httpProvider. defaults.transformrequest = [function (data) {/** * the workhorse; Converts an object to x-www-form-urlencoded serialization . * @param {Object} obj * @return {String} */var param = function (obj) {var query = '; var 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]; if (subvalue! = null) {//Fullsubname = name + ' [' + subn Ame + '] '; Fullsubname = name + '. ' + subname; Fullsubname = SubName; Innerobj = {}; Innerobj[fullsubname] = Subvalue; Query + = param (innerobj) + ' & '; }}} and else if (value!== undefined)//&& Value!== Null {query + = encodeURIComponent (name) + ' = ' + Encodeuricomp Onent (value) + ' & '; }} return query.length? Query.substr (0, query.length-1): query; }; return Angular.isobject (data) && String (data)!== ' [Object File] '? Param (data): data; }]; })
Thanks, finally found the JSON data post to the server transfer mode, no longer reported cross-domain access and server receive format errors
, the problem of multiple parentheses bores me, ({' {' {' params1 ': ' xxxx ', ' params2 ': ' cxxxxxx '} ', '} '), finally solved,
White is turned into application/x-www-form-urlencoded, the parameters are added &
Or the data of your post request, defined by data: {"PARAMS1": "xxxx", "PARAMS2": "Cxxxxxx"} changed to Params1= "xxxx" ¶ms2= "xxxx",
1. The following is my definition, mainly used to solve cross-domain access, is the standard way Angularjs Cors provides
$httpProvider. Defaults.usexdomain = true;
Delete $httpProvider. defaults.headers.common[' X-requested-with '];
$httpProvider. defaults.headers.post[' content-type '] = ' application/x-www-form-urlencoded;charset=utf-8 ';
$httpProvider. defaults.headers.put[' content-type '] = ' application/x-www-form-urlencoded;charset=utf-8 ';
2.
$httpProvider. Defaults.transformrequest this definition is rewritten,
Help us complete the JSON data request to params1= "xxxx" ¶ms2= "XXXX" format
Angular post JSON data to server, cross-domain access, curly braces