Introduction to processing ajax returned arrays by js

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.