欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js對(duì)ajax返回?cái)?shù)組的處理介紹

 更新時(shí)間:2014年02月20日 09:05:16   作者:  
本篇文章主要是對(duì)js對(duì)ajax返回?cái)?shù)組的處理進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助

引言:
ajax異步傳輸,可以傳輸字符串,但是數(shù)組這樣的數(shù)據(jù),就不太好傳遞了,這個(gè)時(shí)候怎么辦呢?

答案是可以通過(guò)json來(lái)處理,后臺(tái)將數(shù)據(jù)數(shù)據(jù)進(jìn)行json編碼!

然后客戶(hù)端,通過(guò)js來(lái)進(jìn)行解析。

這樣問(wèn)題就解決了!json是一種很好的數(shù)據(jù)格式!

我做的是異步判斷某堂課是否處于上課狀態(tài),會(huì)從后臺(tái)接口中獲取數(shù)組數(shù)據(jù)!共四堂課,

代碼如下:

復(fù)制代碼 代碼如下:

function ajaxcheckedlessonsAction(){
        //3583 語(yǔ)文 8班
        //1500 語(yǔ)文 9班
        //2717 物理 8班
        //1612 物理 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;
    }

接口處理數(shù)據(jù),將數(shù)據(jù)json化,

前臺(tái)對(duì)json數(shù)據(jù)進(jìn)行解析

復(fù)制代碼 代碼如下:

//進(jìn)入頁(yè)面就進(jìn)行的處理
        $.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");
                }
            }
        });

紅色部分就是json的核心代碼。

這樣就完成了數(shù)組的處理!異步數(shù)組數(shù)據(jù)傳遞!

相關(guān)文章

最新評(píng)論