Ajax front-end request data processing
SPRINGMVC Background Accept Request data
Springmvc to back to front end data processing
Ajax front-end processing of data returned in the background
Note the point:
Parameter description for 1.ajax submission
Datatype:
Expected data type returned by the server. If not specified, jquery is automatically based on the
HTTP packet MIME information to intelligently judge
"JSON": Return JSON data
"Text": Returns a plain text string
ContentType:
Default value: "Application/x-www-form-urlencoded". The content encoding type when sending information to the server.
Async:
Default value: True. By default, all requests are asynchronous requests. If you need to send a synchronization request, set this option to false.
Success:
The callback function after the request succeeds. Function (A,B,C) has 3 parameters, A is the returned data, B is "success", and C is unknown. The number of formal parameters can be arbitrarily filled in, sequentially loaded arguments
2. Use @responsebody annotations to import at the same time
Jackson-core-asl-1.8.8.jar
Jackson-mapper-asl-1.8.8.jar
The function of this annotation will not take effect
Mode 1:
Ajax data is the format of the URL suffix
Attention:
This way,
When the submission method uses post
ContentType: "Application/json; Charset=utf-8 ", the background cannot get the value
ContentType: "application/x-www-form-urlencoded", background can get the value
When the Submit method uses get
Both can get the value
Front desk:
function AjaxTest1 () {
$.ajax ({
Data: "Name= Little Red &age=12",
Type: "POST",
DataType: ' JSON ',
URL: "Ajax/test1",
Success:function(data) {
Alert ("Success");
alert (data.name);
alert (data.age);
},
Error:function(data) {
Alert ("fail");
}
});
}
Background:
@RequestMapping (value= "Ajax/test1")
public @ResponseBody map<string,object> login1 (httpservletrequest request,httpservletresponse Response) throws ioexception{
System. out. println (Request.getparameter ("name"));
System. out. println (Request.getparameter ("Age"));
map<string,object> map = new hashmap<string,object> ();
Map.put ("Name", Request.getparameter ("name"));
Map.put ("Age", Request.getparameter ("Age"));
return map;
}
Mode 2:
Ajax data is in JSON format
Attention:
This way,
When the submission method uses post
ContentType: "Application/json; Charset=utf-8 ", the background cannot get the value
ContentType: "application/x-www-form-urlencoded", the background can get the value. And Chinese is not garbled
When the Submit method uses get
Both can get values, but Chinese is garbled
Front desk:
function AjaxTest2 () {
$.ajax ({
data:{"name": "Little Red", "Age": 21},
Type: "POST",
DataType: ' JSON ',
URL: "Ajax/test2",
Success:function(data) {
Alert ("Success");
alert (data.name);
alert (data.age);
},
Error:function(data) {
Alert ("fail");
}
});
}
Background:
@RequestMapping (value= "Ajax/test2")
public @ResponseBody map<string,object> login2 (httpservletrequest request,httpservletresponse Response) throws ioexception{
System. out. println (Request.getparameter ("name"));
System. out. println (Request.getparameter ("Age"));
map<string,object> map = new hashmap<string,object> ();
Map.put ("Name", Request.getparameter ("name"));
Map.put ("Age", Request.getparameter ("Age"));
return map;
}
Mode 3:
Ajax data for JSON to string
Attention:
1. The key of the foreground JSON data must correspond to the background bean
So the following empty will be an error
var data = {"Name": "Little Red", "age": "Sex": "Female"};
var data = {"Name": "Little Red", "sex": "Female"};
In the foreground JSON data, key can be the default
So the following conditions are correct
var data = {"Name": "Little Red"};
[email protected] No need to import additional jar packages
3.contentType must be declared as "application/json;charset=utf-8", otherwise it will be error
Front desk:
function AjaxTest3 () {
var data = {"Name": "Little Red", "Age": 12};
$.ajax ({
URL: "Ajax/test3",
Data:JSON.stringify (data),
ContentType: "Application/json;charset=utf-8",
Type: "POST",
Success:function(data) {
Alert ("Success");
alert (data.name);
alert (data.age);
},
Error:function(data,b,c) {
Alert ("fail");
alert (b);
}
});
}
Background:
@RequestMapping (value= "Ajax/test3")
public @ResponseBody person login3 (@RequestBody person aa) throws ioexception{
System. out. println (AA);
return AA;
}
To customize a person class:
Public class person {
Private String name;
Private int age;
Public String GetName () { return name; }
Public void setName (String name) { this. Name = name; }
Public int getage () { return age; }
Public void setage (int age) { this. Age = age; }
}
Mode 4:
Background using HashMap to accept values from the previous segment
Mode 5:
The background does not use @respone to automatically convert JSON data, while using the Respone,write method to write
Springmvc Ajax Value-passing problems