jQuery manual address: https://jquery.cuishifeng.cn/
get request
$.get("test.json", {
"id": "10001",
"name": "小白",
"age": "18岁"
}, function (data) {
console.log(data)
})
Receive two parameters, the first is the URL address, the second is the callback function
You can also receive three parameters, you can add the content of queryString through JSON
note:
- The order can’t be messy, the first parameter is the url address, the second parameter is the queryString data (in JSON format), and the third parameter is the callback function.
- If a page that does not exist in jQuery is requested, the console will report an error and will not continue to execute the content of the callback function later
Requested data back jQuery has helped us to encapsulate data analysis, the original raw Ajax of responseText string parsing returned become readable JSON data structures, no further eval and JSON.parse like operated
post request
The post request usage method and the get request usage method and precautions are the same
$.post("test.json", {
"id": 10005,
"name": "小诶",
"age": "23岁"
}, function (data) {
console.log(typeof data)
})
$.ajax() request
The usage method of $.ajax() request is the same as the basic usage method of get and post, and the function is refined.
$.ajax("test.json", {
"type": "post",
"data": {
"id": "10001",
"name": "小白",
"age": "18岁"
},
"success": function (data, textStatus) {
console.log(data, textStatus)
},
"error": function () {
}
})
- The first parameter refers to the requested file address
- The second parameter is a JSON data, and there are some fixed parameters inside
- type: is the request type
- data: parameters carried in the current request
- success: callback function for successful request
- error: callback function for request failure