Spring MVC Controller and jquery Ajax request processing JSON

Source: Internet
Author: User

When writing an application with spring MVC, we found that the "JSON array object" parameter of jquery was not received in the background, the processing of multiple orders, the AJAX request:

varcmd = {orders:[{"storeId":"0a1","Address":"2nd West Avenue","Goods":[{"Goodsid":"1"}, {"Goodsid":"2"}, {"Goodsid":"3"}]},{"storeId":"0a1","Address":"2nd West Avenue","Goods":[{"Goodsid":"4"}, {"Goodsid":"4"}, {"Goodsid":"5"}]}]} $.ajax ({url:url, type:"POST", datatype:"JSON", Data:cmd, success:function (data, stats) {if(Stats = ="Success") {                        //window.location.href= "/yc"}}, Error:function (data) {alert ("request failed");  }              }); 

Start how to pass the background with the @RequestParam to receive how can not be received, analysis of the request header, the original JSON format is not converted

Origin:http://localhostReferer:http://Localhost/test/myorderuser-agent:mozilla/5.0(Windows NT6.1) applewebkit/537.1(khtml, like Gecko) chrome/21.0.1180.89safari/537.1X-requested-with:xmlhttprequest Form Dataview URL encoded orders[0][storeid]:0a1 orders[0][address]: 2nd West Gate orders[0][goods][0][GOODSID]:1orders[0][goods][1][GOODSID]:2orders[0][goods][2][GOODSID]:3orders[1][storeid]:0a1 orders[1][address]: 2nd West Gate orders[1][goods][0][GOODSID]:4orders[1][goods][1][GOODSID]:4orders[1][goods][2][GOODSID]:5Response Headersview Source Content-length:1051Content-type:text/html;charset=utf-8Date:mon, -Nov -  -:Ten: -GMT Server:apache-coyote/1.1  

By observing, ORDERS[0][STOREID]:0A1
Orders[0][address]: 2nd West Gate
Orders[0][goods][0][goodsid]:1 became a multi-dimensional array of the format passed, not by the way JSON objects passed (originally thought that jquery will automatically turn, but I think more, so there are oolong).

Under a jquery JSON plug-in, Jquery.json-2.4.js turned the JSON, but the problem again, the format is correct but the background is still not received, print the next Request.getparametermap (), parameters in JSON format, But then the way there is a problem, with a string to be sure no, string array can not be connected, list also can not connect, online check related controller of the way to receive JSON objects, need to use @requestbody to receive, in order to facilitate simply Orders stripped directly with JSON array

varcmd = [{"storeId":"0a1","Address":"2nd West Avenue","Goods":[{"Goodsid":"1"}, {"Goodsid":"2"}, {"Goodsid":"3"}]},{"storeId":"0a1","Address":"2nd West Avenue","Goods":[{"Goodsid":"4"}, {"Goodsid":"4"}, {"Goodsid":"5"}]}]

Backstage use

(@RequestBody List orders)  

Check the parameters of the wording, it should be true, after the test still has a problem reported such an error:
POST Http://localhost/test/order 415 (Unsupported Media Type) This issue should be that the foreground request does not indicate Contenttype,ajax plus contenttype: " Application/json; Charset=utf-8 ", again testing, sure enough. The complete code is as follows:
Ajax:

varcmd = [{"storeId":"0a1","Address":"2nd West Avenue","Goods":[{"Goodsid":"1"}, {"Goodsid":"2"}, {"Goodsid":"3"}]},{"storeId":"0a1","Address":"2nd West Avenue","Goods":[{"Goodsid":"4"}, {"Goodsid":"4"}, {"Goodsid":"5"}]}] $.ajax ({url:url, type:"POST", datatype:"JSON", ContentType:"Application/json; Charset=utf-8", Data: $toJSON (cmd), success:function (data, stats) {if(Stats = ="Success") {                        //window.location.href= "/yc"}}, Error:function (data) {alert ("request failed");  }              }); 

Controller Method:

" /order ", method = requestmethod.post)   public  modelandview order (@RequestBody list< map<string,object>> orders) {          System. out. println ("orders Size:" + orders.size ());  }  

The above hope can provide some help to the friend who encounters the similar problem.

Spring MVC Controller and jquery Ajax request processing JSON

Spring MVC Controller and jquery Ajax request processing JSON

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.