ajax php

文章目录

php安装,后台处理脚本语言。

后端开发语言不能直接允许,必须放在服务器对对应的文件夹下运行。

如:wamp的对应服务器的文件夹是www


get请求

c 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>02-get</title>
</head>
<body>

<form action="02-get.php" method="get">
    <input type="text" name="userName"><br>
    <input type="password" name="userPwd"><br>
    <input type="submit" value="提交"><br>

</form>


</body>
</html>
c 复制代码
<?php
//print_r($_GET);//查看可以知道这回得到一个Array的数据
echo $_GET["userName"];
echo $_GET["userPwd"];

?>



post

c 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>02-post</title>
</head>
<body>

<form action="02-post.php" method="post">
    <input type="text" name="userName"><br>
    <input type="password" name="userPwd"><br>
    <input type="submit" value="提交"><br>

</form>

</body>
</html>
c 复制代码
<?php

//print_r($_GET);//查看可以知道这回得到一个Array的数据
echo $_POST["userName"];
echo $_POST["userPwd"];

?>

get和post的异同点



ajax

原生步骤

  1. 创建异步对象
  2. 设置请求方式和请求地址
  3. 发送请求
  4. 监听状态变化

  5. 处理返回的结果

代码及现象:

c 复制代码
 //4.监听状态的变化,状态变化都会执行
                xmlhttp.onreadystatechange = function () {



        		}
c 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04-ajax-get</title>
    <!--
    1.什么是Ajax


    -->
    <script >
        window.onload = function () {
            var oBtn= document.querySelector("button");
            oBtn.onclick = function (evl) {
                //1.需要创建异步对象
                var xmlhttp = new XMLHttpRequest();
                //2.设置请求方式和请求地址
                xmlhttp.open("GET","04-ajax-get.php",true);
                //3.发生请求
                xmlhttp.send();
                //4.监听状态的变化,状态变化都会执行
                xmlhttp.onreadystatechange = function () {
                    if(xmlhttp.readyState === 4){
                        //判断是否请求成功
                        if(xmlhttp.status >= 200 && xmlhttp.status < 300 ||
                            xmlhttp.status === 304){
                            //5.处理返回的结果
                            console.log("接收到服务器返回的数据");
                        }
                        else {
                            //5.处理返回的结果
                            console.log("没有接收到服务器返回的数据");
                        }
                    }

                }


            }
        }
    </script>
</head>
<body>

<button>发生请求</button>
</body>
</html>
c 复制代码
<?php
echo "ajax get test"
?>

完整步骤代码及结果:

c 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04-ajax-get</title>
    <!--
    1.什么是Ajax


    -->
    <script >
        window.onload = function () {
            var oBtn= document.querySelector("button");
            oBtn.onclick = function (evl) {
                //1.需要创建异步对象
                var xmlhttp = new XMLHttpRequest();
                //2.设置请求方式和请求地址
                xmlhttp.open("GET","04-ajax-get.php",true);
                //3.发生请求
                xmlhttp.send();
                //4.监听状态的变化,状态变化都会执行
                xmlhttp.onreadystatechange = function () {
                    if(xmlhttp.readyState === 4){
                        //判断是否请求成功
                        if(xmlhttp.status >= 200 && xmlhttp.status < 300 ||
                            xmlhttp.status === 304){
                            //5.处理返回的结果
                            //console.log("接收到服务器返回的数据");
                            alert(xmlhttp.responseText);
                        }
                        else {
                            //5.处理返回的结果
                            //console.log("没有接收到服务器返回的数据");
                        }
                    }
                }
            }
        }
    </script>
</head>
<body>

<button>发生请求</button>
</body>
</html>
c 复制代码
<?php
echo "ajax get test"
?>

jquery步骤


优点

小写也可正常发送

相关推荐
行思理44 分钟前
JIT+Opcache如何配置才能达到性能最优
c++·php·jit
TDengine (老段)1 小时前
TDengine 做为 Spark 数据源
大数据·数据库·物联网·ajax·spark·时序数据库·tdengine
皓月盈江9 小时前
Linux电脑本机使用小皮面板集成环境开发调试WEB项目
linux·php·web开发·phpstudy·小皮面板·集成环境·www.xp.cn
向哆哆19 小时前
Netty在Java网络编程中的应用:实现高性能的异步通信
java·网络·php
Rverdoser21 小时前
代理服务器运行速度慢是什么原因
开发语言·前端·php
xzkyd outpaper21 小时前
okhttp原理
okhttp
Spider Cat 蜘蛛猫21 小时前
【一】浏览器的copy as fetch和copy as bash的区别
javascript·ajax·bash·逆向·fetch
森叶1 天前
从 JIT 即时编译一直讲到CGI|FastGGI|WSGI|ASGI四种协议的实现细节
python·php·web
myusa21 天前
使用阿里云CLI跨地域迁移ECS实例
数据库·阿里云·php
巴巴_羊1 天前
AJAX原理
前端·javascript·ajax