Now in the project to use the SPRINGMVC framework, need to receive the request from the app JSON data, in order to test convenience, so directly first with the Ajax test, but just beginning with the usual Ajax method, the submission request will appear 415 or 400 error, after research, finally can, Now make a summary.
JS Code:
Function postsimpledata () { $.ajax ({ type: "POST", url: "/service/simpledata", contentType: "Application/json", //must have dataType: "JSON", //represents the return value type, does not have to data: json.stringify ({ ' foo ': ' Foovalue ', ' bar ': ' Barvalue ' }), //equivalent to //data: "{' str1 ': ' Foovalue ', ' str2 ': ' Barvalue '} ', success: function (Jsonresult) { alert (Jsonresult); } }); } function postliststring () { $.ajax ({ type: "POST", url: "/service/liststring", contenttype: "Application/json", datatype: "JSON", data: Json.stringify ({ "buids": ["1", "2", "3"] }), success: function (Jsonresult) { alert (Jsonresult); } }); } function postemployees () { $.ajax ({ type: "POST", url: "/service/employees", contentType: "Application/json", dataType: "JSON", data: json.stringify ({ "Employees": [ { "FirstName": "Bill", "LastName": "Gates" }, { " FirstName ": " George ", " LastName ": " Bush " }, { "FirstName": "Thomas", " LastName ": " Carter " } ] }), success: function (Jsonresult) { alert (Jsonresult); } }); }
Java controller Code:
@RequestMapping (value = "/simpledata", Method=requestmethod.post) @ResponseBodypublic actionresult simpledata (string foo, string bar) { Return json ("Simpledata", jsonrequestbehavior.allowget);} @RequestMapping (value = "/liststring", Method=requestmethod.post) @ResponseBodypublic Actionresult liststring (list<string> buids) { return json (" ListString ", jsonrequestbehavior.allowget);} @RequestMapping (value = "/employees", Method=requestmethod.post) @ResponseBodypublic actionresult employees (list<employee> employees) { return json ("Employees", jsonrequestbehavior.allowget);}
public class Employee{public string FirstName {get; set;} public string LastName {get; set;}}
It is worth noting that there are 2 points:
1) in Ajax options
ContentType: "Application/json"
This must be written to indicate that the data type of request is JSON.
and
DataType: "JSON"
This indicates the type of the return value, which is not required and depends on the return value type.
2) in the options
Data:JSON.stringify ({' foo ': ' Foovalue ', ' bar ': ' Barvalue '})
Most of the time we write data:
{' foo ': ' Foovalue ', ' bar ': ' Barvalue '}
This results in an error, because JS will default to the JSON object in the form data, causing the controller to receive it.
There are two ways to handle this: the first is to use the json.stringify () function, where JSON is defined as a global object by ECMASCRIPT5.
The second way is to wrap them directly in double quotes, such as data: "{' str1 ': ' Foovalue ', ' str2 ': ' Barvalue '}".
Method of post-submission JSON-type data to SPRINGMVC controller via Ajax