1、php上传文件
html文件
<html>
<style type="text/css">
</style>
<script type="text/javascript">
</script>
<head></head>
<body>
<form action="http://127.0.0.1/ex01/mytest.php" method="post" enctype="multipart/form-data">
<input type="file" name="upfile"><br/>
<input type="submit" value="上传"><br/>
</form>
</body>
</html>
php文件
<?php
$fileInfo=$_FILES["upfile"];
print_r($fileInfo);
$fileName=$fileInfo["name"];
$filePath= $fileInfo["tmp_name"];
move_uploaded_file($filePath,"./sources/".$fileName);
?>
2、完整的ajax发送异步请求的过程
<html>
<style type="text/css">
</style>
<script type="text/javascript">
window.onload=function(event){
var obtn=document.querySelector("button");
obtn.onclick=function(event1){
//1、创建一个异步对象
var xmlhttp=new XMLHttpRequest();
//2、设置一个请求方式和请求地址
xmlhttp.open("get","http://127.0.0.1/ex01/mytest.php",true);
//3、发送请求
xmlhttp.send();
//4、监听状态的变化
xmlhttp.onreadystatechange=function(event2){//监听到变化后调用此函数
//判断请求是否成功
if(xmlhttp.status>=200 && xmlhttp.status<300 || xmlhttp.status==304){
//5、返回处理的结果
if(xmlhttp.readyState==4){
console.log("接收到服务器返回的数据");
}
}
}
}
}
</script>
<head></head>
<body>
<button>发送请求</button>
</body>
</html>
3、ajax发送异步请求的过程2
html文件
<html>
<style type="text/css">
</style>
<script type="text/javascript">
window.onload=function(event){
var obtn=document.querySelector("button");
obtn.onclick=function(event1){
//1、创建一个异步对象
var xhr=new XMLHttpRequest();
//2、设置一个请求方式和请求地址
xhr.open("GET","http://127.0.0.1/ex01/mytest.php",true);
//3、发送请求
xhr.send();
//4、监听状态的变化
xhr.onreadystatechange=function(event2){//监听到变化后调用此函数
//判断请求是否成功
if(xhr.readyState==4){
//5、返回处理的结果
if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
console.log("请求成功");
console.log(xhr.responseText);
}
else{
console.log("请求失败");
}
}
}
}
}
</script>
<head></head>
<body>
<button>发送请求</button>
</body>
</html>
php文件
<?php
echo "ajax get page";
?>
打印
请求成功
ajax get page
4、ajax使用POST方式发送异步请求
html文件
<html>
<style type="text/css">
</style>
<script type="text/javascript">
window.onload=function(event){
var obtn=document.querySelector("button");
obtn.onclick=function(event1){
//1、创建一个异步对象
var xhr=new XMLHttpRequest();
//2、设置一个请求方式和请求地址
xhr.open("POST","http://127.0.0.1/ex01/mytest.php",true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
//3、发送请求
xhr.send("username=zhangsan&password=123456");//字符串是传入的数据
//4、监听状态的变化
xhr.onreadystatechange=function(event2){//监听到变化后调用此函数
//判断请求是否成功
if(xhr.readyState==4){
//5、返回处理的结果
if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
console.log("请求成功");
console.log(xhr.responseText);
}
else{
console.log("请求失败");
}
}
}
}
}
</script>
<head></head>
<body>
<button>发送请求</button>
</body>
</html>
php文件
<?php
echo $_POST["username"];
echo "<br/>";
echo $_POST["password"];
?>
打印
请求成功
zhangsan
123456
5、jQuery方式ajax使用POST方式发送异步请求
html文件
<html>
<style type="text/css">
</style>
<script language="javascript" src="jquery-3.5.1.js"></script><!-- 引入jquery库-->
<script type="text/javascript">
window.onload=function(event){
var obtn=document.querySelector("button");
obtn.onclick=function(event1){
$.ajax({
type:"GET",
url:"http://127.0.0.1/ex01/mytest.php",
data:"username=lisi&location=Boston",
success:function(msg){
console.log("Data sage"+msg);
},
error:function(xhr){
console.log(xhr.status);
}
});
}
}
</script>
<head></head>
<body>
<button>发送请求</button>
</body>
</html>
php文件
<?php
echo $_GET["username"];
echo "<br/>";
echo $_GET["location"];
?>
打印:
Data sagelisi
Boston