JS to the AJAX return array processing introduction _AJAX related

Source: Internet
Author: User

Introduction:
Ajax asynchronous transmission, can transmit strings, but the array of such data, it is not very good to pass, this time how to do?

The answer is that you can use JSON to process the data in the background JSON encoding!

Then the client, through JS to parse.

So the problem is solved! JSON is a good data format!

What I do is to judge whether a class is in class or not, and get the array data from the backend interface! A total of four classes,

The code is as follows:

Copy Code code as follows:

function Ajaxcheckedlessonsaction () {
3583 languages, Class 8.
1500 languages, Class 9.
2717 Physics, Class 8.
1612 physics, Class 9.
$whereLessons = Array ();
$whereLessons [' Lessons.id in (?) '] = Array (3583, 1500, 2717, 1612);
$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 processes data, JSON the data,

The foreground parses the JSON data

Copy Code code as follows:

The process of entering the page
$.ajax ({
Type: "POST",
URL: "/default/index/ajaxcheckedlessons",
Data: "",
Success:function (response) {
if (response) {
var data = eval (' (' +response+ ') ');
if (data[' 8yuwen ']) {
$ ("#8yuwen"). attr ("style", "color:red;");
}else{
$ ("#8yuwen"). attr ("Style", "");
}
if (data[' 8wuli ']) {
$ ("#8wuli"). attr ("style", "color:red;");
}else{
$ ("#8wuli"). attr ("Style", "");
}
if (data[' 9yuwen ']) {
$ ("#9yuwen"). attr ("style", "color:red;");
}else{
$ ("#9yuwen"). attr ("Style", "");
}
if (data[' 9wuli ']) {
$ ("#9wuli"). attr ("style", "color:red;");
}else{
$ ("#9wuli"). attr ("Style", "");
}
}else{
Alert ("Error");
}
}
});

The red part is the core of the JSON code.

This completes the processing of the array! Asynchronous Array Data Delivery!

Related Article

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.