Introduction:
Ajax asynchronous transmission can transmit strings, but data such as arrays is not very easy to transmit. What should we do at this time?
The answer is that the data can be processed in json format, and the data is encoded in json format in the background!
Then, the client uses js for parsing.
The problem is solved! Json is a good data format!
What I am doing is to asynchronously determine whether a class is in the class status and retrieve array data from the background interface! There are four classes in total,
The Code is as follows:
Copy codeThe Code is as follows:
Function ajaxcheckedlessonsAction (){
// 3583 Class 8
// Class 9 of 1500 Chinese
// 2717 physical 8 Class
// 1612 physical Class 9
$ WhereLessons = array ();
$ WhereLessons ['lessons. id IN (?) '] = Array (3583,150 0, 2717,161 2 );
$ DaoLessons = new dao_lessons ();
$ Alllessons = $ daoLessons-> getLessons ($ whereLessons );
// Print_r ($ alllessons); exit;
$ LessonsChecked = array ();
Foreach ($ alllessons as $ lessons ){
If ($ lessons ['id'] = 3583 ){
$ LessonsChecked ['8yuwen'] = $ this-> verifyCheckedLessons ($ lessons ['startime'], $ lessons ['endtime']);
} Elseif ($ lessons ['id'] = 1500 ){
$ LessonsChecked ['9yuwen'] = $ this-> verifyCheckedLessons ($ lessons ['startime'], $ lessons ['endtime']);
} Elseif ($ lessons ['id'] = 2717 ){
$ LessonsChecked ['8wuli'] = $ this-> verifyCheckedLessons ($ lessons ['startime'], $ lessons ['endtime']);
} Elseif ($ lessons ['id'] = 1612 ){
$ LessonsChecked ['9wuli'] = $ this-> verifyCheckedLessons ($ lessons ['startime'], $ lessons ['endtime']);
}
}
$ Json = json_encode ($ lessonsChecked );
Echo $ json;
}
Interface to process data, json data,
Parse json data at the front end
Copy codeThe Code is as follows:
// Process the operation on the page
$. Ajax ({
Type: "POST ",
Url: "/default/index/ajaxcheckedlessons ",
Data :"",
Success: function (response ){
If (response ){
Var data = eval ('+ response + ')');
If (data ['8yuwen']) {
$("#8 yuwen"). attr ("style", "color: red ;");
} Else {
$("#8 yuwen"). attr ("style ","");
}
If (data ['8wuli']) {
$("#8 wuli"). attr ("style", "color: red ;");
} Else {
$("#8 wuli"). attr ("style ","");
}
If (data ['9yuwen']) {
$("#9 yuwen"). attr ("style", "color: red ;");
} Else {
$("#9 yuwen"). attr ("style ","");
}
If (data ['9wuli']) {
$("#9 wuli"). attr ("style", "color: red ;");
} Else {
$("#9 wuli"). attr ("style ","");
}
} Else {
Alert ("error ");
}
}
});
The red part is the core code of json.
This completes Array Processing! Asynchronous array data transmission!