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步骤


优点

小写也可正常发送

相关推荐
夜色呦20 分钟前
Spring Boot实现新闻个性化推荐
服务器·spring boot·php
zpjing~.~23 分钟前
Vue 多次尝试请求ajax
前端·vue.js·ajax
ac-er888812 小时前
PHP反射机制
开发语言·php
luthane14 小时前
python 实现dijkstra迪杰斯特拉算法
python·算法·php
hadagaga15 小时前
Session反序列化漏洞解析
网络安全·php
Jason-河山19 小时前
PHP中的HTTP请求:获取taobao商品数据的艺术
开发语言·http·php
ac-er888820 小时前
PHP预处理机制详解
开发语言·php
拾伍廿肆20 小时前
PHP反射
php
dawn19122820 小时前
Axios 和 Ajax 的区别与联系
java·前端·javascript·ajax·okhttp
有谁看见我的剑了?1 天前
ubuntu 系统安装
数据库·ubuntu·php