V. Server-side SPRINGMVC How to return a JSON-type string.
Request:
$ ("#testJson8"). Click (function () {$.ajax ({url: "Testreturnjsonvalue", type: "Post", Success:fu Nction (Result) {Console.log (result); } });});
1. Returning a single object
Handler method:
@ResponseBody @requestmapping ("/testreturnjsonvalue") public person Testreturnjsonvalue () {Person person = new person () ; Person.setname ("Lily"); Person.setage (23); return person;}
The person object is printed normally in the browser console.
Note: DataType is not specified here.
2. Returning multiple objects
Handler method:
@ResponseBody @requestmapping ("/testreturnjsonvalue") public list<person> Testreturnjsonvalue () {list< person> personlist = new arraylist<> (); person person = new person (); Person.setname ("Lily"); Person.setage (23); Person Person2 = new person (); Person2.setname ("Lucy"); Person2.setage (33); Personlist.add (person); Personlist.add (Person2); return personlist;}
The person array is printed normally in the browser control bar.
3. Return to Map
@ResponseBody @requestmapping ("/testreturnjsonvalue") public map<string, person> Testreturnjsonvalue () {Map <string, person> map = new hashmap<> (); person person = new person (); Person.setname ("Lily"); Person.setage (23); Person Person2 = new person (); Person2.setname ("Lucy"); Person2.setage (33); Map.put ("1", person); Map.put ("2", Person2); return map;}
Browser Console output:
4. The Ajax return value in the actual production environment.
Encapsulates a return value type:
public class Ajaxresult implements Serializable {
public static final String result_code_0000 = "0000";
public static final String result_code_0001 = "0001";
Private String Code;
Private String message;
Private Object data;
Public Ajaxresult () {}
Public String GetCode () {return this.code; }
public void Setcode (String code) {this.code = code; }
Public String GetMessage () {return this.message; }
public void Setmessage (String message) {this.message = message; }
Public Object GetData () {return this.data; }
public void SetData (Object data) {this.data = data; }}
Actual use:
@ResponseBody @requestmapping ("/testreturnjsonvalue")
Public Ajaxresult Testreturnjsonvalue () {Ajaxresult ajaxresult = new Ajaxresult ();
try {map<string, person> Map = new hashmap<> (); person person = new person (); Person.setname ("Lily"); Person.setage (23); Person Person2 = new person (); Person2.setname ("Lucy"); Person2.setage (33); Map.put ("1", person); Map.put ("2", Person2); Ajaxresult.setdata (map); Ajaxresult.setmessage ("success!"); Ajaxresult.setcode (ajaxresult.result_code_0000); } catch (Exception e) {e.printstacktrace (); Ajaxresult.setmessage ("fail!"); Ajaxresult.setcode (ajaxresult.result_code_0001); } return Ajaxresult;}
Vi. Request Payload
(1) Conditions of occurrence:
ContentType: ' Application/json;charset=utf-8 '
Type:post
(2) For details, please refer to
http://xiaobaoqiu.github.io/blog/2014/09/04/form-data-vs-request-payload/
(3) It is recommended to try not to handle this situation manually, can choose other ways to avoid as far as possible.
Vii. Summary
This paper mainly introduces the support of SPRINGMVC to Ajax, how to organize the AJAX data, and emphatically introduces the support of the form.
springmvc-Processing of Ajax (with JSON type) (3)