Angular post JSON data to server, cross-domain access, curly braces

Source: Internet
Author: User

 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" &params2= "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" &params2= "XXXX" format






Angular post JSON data to server, cross-domain access, curly braces

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.