本篇文章为大家展示了怎么在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数组,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网行业资讯频道。