0x01.
Using $.post ()/$.get ()/$.ajax () in jquery ()/$.load () This is a common four ways to interact with the background of Ajax
General idea:
The front-end JS Trigger event invokes the jquery function, submits the parameters to the background, processes the data in the background, returns the data result set (JSON format), and the information assigned by the front desk transfer to the foreground page display.
1. Foreground events
Click: Mouse click event Application Range: button, form submit ...
onchange: drop-down list value change <select><option value= "" ></select>
...
Calls to 2.js functions
...
$ (function () {
$ ("button"). Click (function () {
var data= $ ("#form"). Val (); The data received may be one or more
$.load ("url", "Data", function (response,status) {
if (status== "success") {
#处理json数据 assignment to an HTML page
}
if (status== "error") {
#错误信息
}
});
})
})
If this is the Change event in <select>
Modified to $ (function () {
$ ("#select"). Change (function () {
#....
})
})
3. Back end returns JSON data
Public Function Ajax () {
.....
$data ["id"]= "";
$data ["Name"]= "";
$this->ajaxreturn ($data); The method in thinkphp is called directly here
}
Data format:
Single data: {"id": "1", "name": "XXX"}
Multiple array data: {"0": ["id": "1", "Name": "XX"], "1": ["id": "2", "name": "XXX"],...,}
Sometimes with status information: {"0": ["id": "1", "Name": "XX"], "1": ["id": "2", "name": "XXX"],..., "refer": "", "State": "}
0x02.
Processing JSON data traversal
...
Front desk Ajax needs to be aware of the place