Angular post json data to the server, cross-origin access, multiple braces

Source: Internet
Author: User

Angular post json data to the server, cross-origin access, multiple braces

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
 
  

 

Thanks, I finally found the transmission method of the json data post to the server, and won't report the cross-origin access and server receipt format errors any more. I am tired of the problem of multiple brackets, ({'{"params1": "xxxx", "params2": "cxxxxxx"}', ''}), finally solved,

To put it bluntly, convert it to application/x-www-form-urlencoded, and add &

Or the data in your post request is defined by data: {"params1": "xxxx", "params2 ": "cxxxxxx"} changed to params1 = "xxxx" ¶ ms2 = "xxxx ",

 

1. The following is my definition, which is mainly used for cross-origin access. It is a standard method provided by angularjs CORS.

$ 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 is overwritten,

The json data request is converted to params1 = "xxxx" ¶ ms2 = "xxxx" format.

 

 

 

Zookeeper

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.