博主信息
博文 34
粉丝 0
评论 1
访问量 29122
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
JSON 与 Ajax 及实战—2018年9月13日23时45分
感恩的心的博客
原创
813人浏览过

1、问答: Ajax的工作原理分析(用自己语言组织)

答: Ajax的全称是 Asynchronous JavaScript and XML[异步的 JavaScript 和 XML];

主要特性在“异步”上,***端发送请求后,有两种方式:一是同步,等待服务器返回,在此过程中不进行任何操作;二是异步,在等待服务器返回的时间里做其他事情,同时进行多请求操作。

2.编程: Ajax用户登录验证

实例

<!DOCTYPE html>
<html>
    <head>
        <title>Ajax 实战</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h3>用户登录</h3>
        <form action="">
            <p>邮箱:<input type="email" name="email"></p>
            <p>密码:<input type="password" name="password"></p>
            <p><button type="button">提交</button></p>
        </form>        

        <script>
            let btn = document.getElementsByTagName("button")[0];
            btn.onclick = function () {
                //1.创建xhr对象
                let xhr = new XMLHttpRequest();
                //2.监听响应状态
                xhr.onreadystatechange=function(){
                    if(4!==xhr.readyState)return false;
                    if(200!==xhr.status){
                        alert("响应失败:"+xhr.status);
                    }
                    let p=document.createElement('p');
                    p.style.color='red';
                    //获得服务器返回至
                    //let jsonData=JSON.parse(xhr.responseText);
                    console.log(xhr.responseText);
                    let jsonData = JSON.parse(xhr.responseText);
                    p.innerHTML=jsonData.msg;
                    document.getElementsByTagName('form')[0].appendChild(p);
                    btn.disabled=true;
                }

                //3.设置请求参数
                xhr.open('post','inc/check.php',true);

                //4. 设置头信息,将内容类型设置为表单提交方式
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

                //5.发送请求
                let data={
                    email:document.getElementsByName('email')[0].value,
                    password:document.getElementsByName('password')[0].value
                };
                let sendingData='data='+JSON.stringify(data);
                xhr.send(sendingData);

            }



        </script>
    </body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例



php

实例

<?php

//print_r($_POST['data']);
//echo $data['email'];


$user = json_decode($_POST['data']);
//echo $user->email;
$email = $user->email;
$password = sha1($user->password);

$pdo = new PDO('mysql:host=localhost;dbname=php', 'root', 'root123');

$sql = "SELECT COUNT(*) FROM `user` WHERE `email`='{$email}' AND `password`='{$password}' ";

$stmt = $pdo->prepare($sql);

$stmt->execute();

if ($stmt->fetchColumn(0) == 1) {
    echo json_encode(['status' => 1, 'msg' => '登录成功,正在跳转...']);
    exit;
} else {
    echo json_encode(['status' => 0, 'msg' => '邮箱或密码错误,登录失败!']);
    exit;
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


3.手写: Ajax的完整运行流程(以get为例),共四步。

813078428.jpg

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学