ajax submit data type
1. The source of the problem
When using ajax today, I found that when getting data is passed, parameters cannot be passed when passing a json string, so I did some experiments to test the difference between ajax get and post data passing.
2. Concept
Ajax has three ways to pass data:
- json format
- json string format
- Standard parameter mode
1.json format
形如:
{“username”:”chen”,”nickname”:”alien”}
ajax:
$.ajax({
type:"post",
url:"/test/saveUser",
data:{"username":"chen","nickname":"alien"}, dataType:"json", success: function(data){ console.log(data); } });
2. The json string
is like:
"{"username":"chen","nickname":"alien"}" or JSON.stringify({"username":"chen","nickname":"alien"})
ajax:
$.ajax({
type:"post", url:"/test/saveUser", data:JSON.stringify({"username":"chen","nickname":"alien"}), contentType:"json/application" dataType:"json", success: function(data){ console.log(data); } });
The get request parameters in this format cannot be passed, the json string will not be parsed into parameters
and contentType: "json/application" needs to be added
3. Standard parameter mode
形如:
“username=chen&nickname=alien”
ajax:
$.ajax({
type:"post", url:"/test/saveUser", data:"username=chen&nickname=alien", dataType:"json", success: function(data){ console.log(data); } });
$(“#form1”).serialize() is to spell the data of the form into a string in this format!
3. Summary
The post request can pass parameters in types 1, 2, and 3. The
get request can pass parameters in types 1
and 3. $(“#form1”).serialize() is to assemble the name and value of the input in the corresponding form, and finally form 3 types of strings