Blogger Information
Blog 28
fans 1
comment 0
visits 17292
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
2019.5.16作业
关超的博客
Original
684 people have browsed it
1. 如何从服务器端获取JSON格式字符串并解决到前端页面中显示
从服务器返回json格式数据,就是一个字符串,所以可以将php数组或对象转换为json格式字符串进行输出
echo json_encode(......)


2. 使用JSON改写5月15日的表单验证案例
后台PHP内容
if (!in_array($email, $emailList)) {
    echo json_encode(['status'=>1, 'message'=>'邮箱不正确']);
} else if (md5($password) !== $pwd) {
    echo json_encode(['status'=>2, 'message'=>'密码不正确']);
} else {
    echo json_encode(['status'=>3, 'message'=>'验证通过正在跳转']);
}


前台javascript内容

                var obj = JSON.parse(request.responseText);
                console.log(obj);

                switch (obj.status) {
                    case 1:
                        result.style.color = 'red';
                        result.innerHTML = obj.message;
                        break;
                    case 2:
                        result.style.color = 'red';
                        result.innerHTML = obj.message;
                        break;
                    case 3:
                        result.style.color = 'green';
                        result.innerHTML = obj.message;
                        break;
                }


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post