Method of post-submission JSON-type data to SPRINGMVC controller via Ajax

Source: Internet
Author: User

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

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.