Introduction to the processing of Ajax return array by JS

Source: Internet
Author: User
Tags json

  This article is mainly to JS to the Ajax return array processing for a detailed introduction, the need for friends can come to the reference, I hope to help you.

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!   I do is asynchronous to determine whether a class is in the class state, will get the array data from the background interface! A total of four classes,   code as follows:     code is as follows: function ajaxcheckedlessonsaction () {       //3583 language 8 class          //1500 language 9          //2717 physical 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) {            &N Bsp   $lessonsChecked [' 9yuwen '] = $this->verifycheckedlessons ($lessons [' Startime '], $lessons [' Endtime ']);             ElseIf ($lessons [' id '] = = 2717) {            &N Bsp   $lessonsChecked [' 8wuli '] = $this->verifycheckedlessons ($lessons [' Startime '], $lessons [' Endtime ']);             ElseIf ($lessons [' id '] = = 1612) {            &N Bsp   $lessonsChecked [' 9wuli '] = $this->verifycheckedlessons ($lessons [' Startime '], $lessons [' Endtime ']);                     {        $json = Json_encode ( $lessonsChecked);         Echo $json;    }   Interface processing data, JSON data,   foreground to the JSON data parsing     code as follows://Enter the page on the processing of         $ . Ajax ({            type: "POST",             URL: "/default/in Dex/ajaxcheckedlessons ",             data:",             SUCC Ess:function (response) {                if (response) {                    var data = eval (' (' +response+ ') ');                     if (data[' 8yuwen ']) {        &NBS P             $ ("#8yuwen"). attr ("style", "color:red;");                    }else{            &NB Sp         $ ("#8yuwen"). attr ("Style", "");     &nbsp                                   & nbsp if (data[' 8wuli ']) {                      $ ("#8wuli"). attr ("Style", " color:red; ");                    }else{            &NB Sp         $ ("#8wuli"). attr ("Style", "");                                   & nbsp       if (data[' 9yuwen ']) {                      $ ("# 9yuwen "). attr (" style "," color:red; ");                    }else{            &NB Sp         $ ("#9yuwen"). attr ("Style", "");                     &NBSP                     if (data[' 9wuli ']) {        &NB Sp             $ ("#9wuli"). attr ("style", "color:red;");                    }else{            &NB Sp         $ ("#9wuli"). attr ("Style", "");                                   & nbsp  }else{                    alert ("error");       & nbsp        }             {       }); The   red part is the core code for JSON.   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.