网站建设资讯

NEWS

网站建设资讯

怎么在JavaScript中返回ajax数组

本篇文章为大家展示了怎么在JavaScript中返回ajax数组,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

目前创新互联建站已为超过千家的企业提供了网站建设、域名、网络空间、绵阳服务器托管、企业网站设计、犍为网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

代码如下:


function ajaxcheckedlessonsAction(){
        //3583 语文 8班
        //1500 语文 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;
    }


接口处理数据,将数据json化,

前台对json数据进行解析

复制代码 代码如下:


//进入页面就进行的处理
        $.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");
                }
            }
        });

上述内容就是怎么在JavaScript中返回ajax数组,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


新闻名称:怎么在JavaScript中返回ajax数组
文章路径:http://cdweb.net/article/gegpgo.html