Angularjs the conversion of the POST request parameters to the same parameters as Ajax for SPRINGMVC use

Source: Internet
Author: User


In general, the ANGULARJS post format is (my template): The Angularjs request method is:
 
   

  
  1. Content-Type: application/json
The data passed in this way: such as passing an array: Targetarr is the real format of the array is the JSON format, ah, SPRINGMVC is not good to receive: unfold to be like this, In short, the springmvc of the @requestparam can not accept the type: In this case, the first to say that normal can work, not in the JS conversion format: With SPRINGMVC @requestbody to accept JSON data, but the premise is, You have to have a JavaBean file. For example, the corresponding array, written as: And then can receive, generally receive the common database class is still no problem, such as a user object However, if I want to pass in any of the puppy infrequently used data, do they have to create class for them?? In order to solve this problem, it is necessary to switch to Ajax-like jquery, and then @requestparam. The way Ajax is passed is this:
 
   

  
  1. Content-Type=“application/x-www-form-urlencoded"
Then an array of specifics should be like this: if it's an object, it should be:
All in all: what should be converted in Angularjs? Find a lot of information on the Internet, few people say clearly, here grumble about, those people have not shared spirit? The following code is a reference to the Internet, because the online copy to copy, specifically do not know who the original person, thank him. Take his code and change it.app.config is the conversion. Plus in the JS file is.
  

var app = angular.module("MyApp", []);
app.config(function ($httpProvider) {
	$httpProvider.defaults.headers.put[‘Content-Type‘] = ‘application/x-www-form-urlencoded‘;
	$httpProvider.defaults.headers.post[‘Content-Type‘] = ‘application/x-www-form-urlencoded‘;
	// 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 + ‘[]‘;
						innerObj = {};
						innerObj[fullSubName] = subValue;
						query += param(innerObj) + ‘&‘;
					}
				} else if (value instanceof Object) {
					for (subName in value) {
						subValue = value[subName];
						fullSubName = 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;
		};
		return angular.isObject(data) && String(data) !== ‘[object File]‘
			? param(data)
			: data;
	}];
});
Well, with the above paragraph, put Angularjs's post,get request for the method of jquery. This is a function of JS's angular: Pass the parameter: Then, receive at the Springmvc end: OK, get it here, why do it!!






From for notes (Wiz)


Angularjs the conversion of the POST request parameters to the same parameters as Ajax for SPRINGMVC use


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.