This is my backstage SPRINGMVC controller receives the Isform parameter the method, simply hits its value:
@RequestMapping (method = requestmethod.post)
@ResponseBody
The public map<string, object> Save (
@ Requestparam (value = "Isform", required = False) String isform) {
System.out.println ("Isform value:" + isform);
return null;
}
Front page sends a request for a post submission form
found no value in background
And then I thought about it. The first option is to add Requestbody to the controller method parameter to receive the JSON parameter, which can be changed as follows:
@RequestMapping (method = requestmethod.post)
@ResponseBody
The public map<string, object> Save (
@ Requestparam (value = "Isform", required = False) @RequestBody String isform) {
System.out.println ("Isform Value:" + Isform);
return null;
}
But the value of the isform is still null,
Then I compared the previous project, received the POST request parameters, 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,
The "Content-type" of jquery's 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 like this, but there is still no isform value.
After another half day, in a foreigner's blog found the reason:
By default, the JQuery transmits data using content-type:x-www-form-urlencoded and the familiar Foo=bar&baz=mo E Serialization. Angularjs, however, transmits data using Content-type:application/json and {"foo": "Bar", "Baz": "Moe"} JSON Serializat Ion
I have translated:
By default, jquery transmits data using Content-type:x-www-form-urlencodedand and a sequence similar to "Foo=bar&baz=moe," but Angularjs, The transfer data uses the JSON sequence Content-type:application/json and {"foo": "Bar", "Baz": "Moe"}.
So after setting the Content-type to X-www-form-urlencodedand, you also need to convert the format of the sequence,
Below is I pass the foreigner practice but oneself test of the final plan:
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;
}];
});
Add the above code in the angular module and we'll look at the effect:
Found consistent with jquery's post request style, with wood and!!!
Look at the background of the parameters receive,
Isform has been able to receive the parameters of the normal, done!
The above is the angular POST request background receive parameter NULL solution, hope to help everyone's learning.