SPRINGMVC implementation of Ajax and Restfull

Source: Internet
Author: User

1. Need to open put, and delete support in Web. xml

  <!--browser does not support Put,delete method, the filter converts/xxx?_method=delete to standard HTTP Delete methods-      <filter>        <filter-name>hiddenHttpMethodFilter</filter-name>        <filter-class> org.springframework.web.filter.hiddenhttpmethodfilter</filter-class>    </filter>    < filter-mapping>        <filter-name>hiddenHttpMethodFilter</filter-name>        <url-pattern>/* </url-pattern>    </filter-mapping>   

2. Be sure to import Jackson's jar package

3, Jqery send Ajax to SPRINGMVC, need is a JSON standard format string, not JSON object, so we have to first turn into a string, involving the sending Ajax pits are as follows:

SPRINGMVC sending Ajax

The $.ajax () method is generally used for data transmission, mainly because $.postthe () method sends the data in the form of a JSON object format, while the $.ajax () method can send a string form JSON , , also use springmvc Ajax features to remember to import Jackson 's 2.4 Version above the package, or report 406 error, Several pits to be noted using the $.ajax () method are:

$.ajax ({

URL: "${pagecontext.request.contextpath}/test.action" ,

data:JSON.stringify (obj), // This is also a pit,Springmvc requires a JSON string in strict form and must be guaranteed JSON The string cannot appear deformed

   contenttype: "Application/json;charset=utf-8" , // This is also a pit, which specifies the form of message content sent to the server side, the default urlencoder json json contenttype

Type: "POST" ,

DataType:"JSON",  // Here's a pit. , datatype The data type to be returned for the specified response must be xml json Text html jqery The framework fails to convert the returned data to the specified format, and from the resulting success function cannot be called, but the server can receive the data, also can return normally, and js end parsing data error causing the card to die, but there is no error displaying

Success: function (data)

      {

        

}

});

The server assembles the JSON data into a pojo object, which is achieved by @RequestBody annotations, and the Pojo objects returned as JSON objects are implemented by annotations @ResponseBody , and are written in one by one . Even if you return a string @ResponseBody you can't miss it .

@RequestMapping ( "/test.action " )

Public @ResponseBody user func1 (@RequestBody user u)

return User;

}

Wherein, sends the AJAX ContentType to send the past format,datatype for receives when lets the jqery convert the format, must specify lets it the normal conversion the format to be OK, otherwise does not have the error, But there is no successful callback response.

4, the Code implementation:

①, server, default can not write produces={"Application/json;charset=utf-8"}, produces is the JSON format encoding that specifies the response back to the client, unless the return format is garbled :

@RequestMapping (value="/submit", method=requestmethod.delete,produces={"Application/json;charset=utf-8"})         Public@ResponseBody user submit1 (@RequestBody user u) {System. out. println (U); return NewUser ("004","Jerry"); } @RequestMapping (Value="/submit", method=requestmethod.post) Public@ResponseBody user submit2 (@RequestBody user u) {System. out. println (U); return NewUser ("003","Jerry"); }

②, client:

<script type= "Text/javascript" >$ (function () {$ ("#btn1"). Click (function () {var obj = {uid:1,name: ' Jerry1 '};$. Ajax ({url: ' ${pagecontext.request.contextpath}/submit?_method=delete ', type: ' Post ', data:JSON.stringify (obj), ContentType: ' Application/json;charset=utf-8 ', DataType: ' JSON ', success:function (data) {alert (data.uid);});}); $ ("#btn2"). Click (function () {var obj = {uid:2,name: ' Jerry2 '};$.ajax ({url: ' ${pagecontext.request.contextpath}/ Submit ', type: ' Post ', data:JSON.stringify (obj), ContentType: ' Application/json;charset=utf-8 ', DataType: ' JSON ', Success:function (data) {alert (DATA.UID);}});}); </script>

 The client distinguishes the Restfull method by URL parameters, and the server is restricted by Requestmethod.

SPRINGMVC implementation of Ajax and Restfull

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.