Angular POST request, SPRINGMVC background cannot receive parameter value solution

Source: Internet
Author: User

This is my backstage SPRINGMVC controller receives the Isform parameter the method, just simply hits its value:

@RequestMapping (method = Requestmethod.post)
@ResponseBody
Public map<string, Object> Save (
@RequestParam (value = "Isform", required = False) String Isform) {
System.out.println ("Isform value:" + isform);
return null;

}

The front page sends a request for a post submission form

found that the background did not fetch a value

The first scenario I came up with was to add requestbody to the controller method parameter to receive the JSON parameter and change it to the following:

@RequestMapping(method = RequestMethod.POST)@ResponseBodypublic Map<String, Object> save(@RequestParam(value = "isform", required = false) @RequestBody String isform) {System.out.println("isform value: " + isform);return null;}

But the result of the Isform value is still null,

Then I compared the previous project, received the parameters of the POST request, found an interesting phenomenon,

Below is the default request header for angular,

$httpProvider. Defaults.headers.post: (header defaults for post requests)

Content-type:application/json

$httpProvider. Defaults.headers.put (header defaults for put requests)

Content-type:application/json

where Angular's post and put are Application/json,

and the "Content-type" of the jquery Post request defaults to " application/x-www-form-urlencoded", so I changed the default content-type for angular,

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‘;});

The next request body becomes this, but the back still does not take the value of the Isform,

Another half-day, in a foreigner's blog found the reason, the link is as follows:

http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/

By default, jQuery transmits data using content-type:x-www-form-urlencoded and the familiar Foo=bar&baz=moe Serializa tion. AngularJS, however, transmits data using Content-type:application/json and {"foo": "Bar", "Baz": "Moe"} JSON Serializat Ion

I translated it:

By default, jquery transmits data using Content-type:x-www-form-urlencodedand and sequences similar to "Foo=bar&baz=moe", however Angularjs, Transfer data using Content-type:application/json and {"foo": "Bar", "Baz": "Moe"} such JSON sequence.

So after the Content-type is set to X-www-form-urlencodedand, you also need to convert the format of the sequence,

Below is the final plan that I have tested by the foreigner practice:

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 + ' [' + i + '] ';
Innerobj = {};
Innerobj[fullsubname] = Subvalue;
Query + = param (innerobj) + ' & ';
}
} else if (value instanceof Object) {
For (subname in value) {
Subvalue = Value[subname];
Fullsubname = name + ' [' + 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;
}];
});

To add the above code to the angular module, let's look at the effect:

Discover the same style as the POST request for jquery, with a wooden there!!!

Look at the background of the parameters to receive the situation,

Isform has been able to receive the parameters of the normal, done!

Angular POST request, SPRINGMVC background cannot receive parameter value solution

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.