安装好XAMPP软件,并运行起来。本文代码是基于XAMPP开发环境,XAMPP是彻底免费且易于安装的Apache发行版,其中包含MariaDB、PHP和Perl。XAMPP开放源码包的设置让安装和使用出奇容易。php
前台部分 其中“process.php?name=Herry”,向后台传递name数据ajax
document.getElementById("button").addEventListener("click",function () {
var xhr = new XMLHttpRequest();
xhr.open("GET","process.php?name=Herry",true);
xhr.onreadystatechange=function () {
if(xhr.readyState==4&&xhr.status==200) {
var data = xhr.responseText;
console.log(data)
}
};
xhr.send();
})
复制代码
后台PHP部分 后台接收了name数值,并向前台返回了"GET: 你的名字是". $_GET['name']浏览器
<?php
if (isset($_GET['name'])) {
echo "GET: 你的名字是". $_GET['name'];
}
?>
复制代码
因而最后前台console里面获得:GET: 你的名字是Herry安全
前台部分bash
<form action="process.php" method="GET">
<input type="text" name="name">
<input type="submit" value="提交">
</form>
复制代码
后台PHP部分网络
<?php
if (isset($_GET['name'])) {
echo "GET: 你的名字是". $_GET['name'];
}
?>
复制代码
表单输入名字Bucky,而后点击提交后,浏览器将数据打包后,传递给后台,最后后台返回咱们想要的数据----GET: 你的名字是Bucky。整个过程当中页面有刷新,数据点击提交后,页面跳转到这个网址http://localhost/ajax/process.php?name=Buckyapp
Ajax异步请求数据,无需刷新页面。能够提升用户体验,较少网络数据的传输量。click事件改为submit事件(表单应该用submit事件),而后取消默认事件。框架
前台部分异步
//Html部分
<form id="getForm">
<input type="text"name="name" id="name1">
<input type="submit"value="提交">
</form>
//Javascript部分
document.getElementById("getForm").addEventListener("submit",function(e){
e.preventDefault();//阻止默认跳转事件
var name=document.getElementById("name1").value;//获取输入的值
var xhr = new XMLHttpRequest();
xhr.open("GET","process.php?name="+name,true);
xhr.onreadystatechange=function () {
if ( xhr.status == 200&&xhr.readyState == 4) {
var data = xhr.responseText;
console.log(data);
}
}
xhr.send();
})
复制代码
后台PHP部分post
<?php
if (isset($_GET['name'])) {
echo "GET: 你的名字是". $_GET['name'];
}
?>
复制代码
在表单输入Bucky,点击提交,最后在console显示:GET: 你的名字是Bucky。整个过程页面无刷新,有效提升页面性能。
与GET提交数据差很少 前台部分
<form action="process.php" method="POST">
<input type="text" name="name">
<input type="submit" value="提交">
</form>
复制代码
后台PHP部分
<?php
if (isset($_POST['name'])) {
echo "POST: 你的名字是". $_POST['name'];
}
?>
复制代码
表单输入名字Bucky,而后点击提交后,浏览器将数据打包后,传递给后台,最后后台返回咱们想要的数据----POST: 你的名字是Bucky。整个过程当中页面有刷新,数据点击提交后,页面跳转到这个网址http://localhost/ajax/process.php。与GET方式提交不一样的是,POST方法数据并无内嵌在url中,这样安全性比较高。
POST请求与GET主要有两点不一样:
①post请求必定要设置请求头的格式内容:
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
复制代码
②post请求参数放在send里面,即请求体
xhr.send("name="+name" ); 复制代码
前台部分
//HTML部分
<form id="postForm">
<input type="text"name="name" id="name2">
<input type="submit"value="提交">
</form>
//Javascript部分
document.getElementById("postForm").addEventListener("submit", function (e) {
e.preventDefault();
var name=document.getElementById("name2").value;
var params = "name="+name;
var xhr = new XMLHttpRequest();
xhr.open("POST","process.php",true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.onreadystatechange=function () {
if(xhr.readyState==4&&xhr.status==200) {
var data = xhr.responseText;
console.log(data);
}
};
xhr.send(params);
})
复制代码
后台PHP部分
<?php
if (isset($_POST['name'])) {
echo "POST: 你的名字是". $_POST['name'];
}
?>
复制代码
表单输入名字Bucky,而后点击提交后,最后在console显示:POST: 你的名字是Bucky。整个过程页面无刷新。
1.在不须要从新刷新页面的状况下,Ajax 经过异步请求加载后台数据,提升用户体验和页面性能。 2.GET参数经过URL传递,POST放在Request body中,后者安全性比较高。