AJAX中的POST与GET方式实例
  xVpghvCvc9NK 2023年11月02日 57 0


ajax.html代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "​​http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd​​​">
<html>
<head>
<title>GET VS. POST</title>
<script language="javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject)
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
else if(window.XMLHttpRequest)
xmlHttp = new XMLHttpRequest();
}
function createQueryString(){
var firstName = document.getElementByIdx_x_x_x_x_x("firstName").value;
var birthday = document.getElementByIdx_x_x_x_x_x("birthday").value;
var queryString = "firstName=" + firstName + "&birthday=" + birthday;
return encodeURI(encodeURI(queryString)); //两次编码解决中文乱码问题
}
function doRequestUsingGET(){
createXMLHttpRequest();
var queryString = "ajax.php?";
queryString += createQueryString() + "×tamp=" + new Date().getTime();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET",queryString);
xmlHttp.send(null);
}
function doRequestUsingPOST(){createXMLHttpRequest();
var url = "ajax.php?timestamp=" + new Date().getTime();
var queryString = createQueryString();
xmlHttp.open("POST",url);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(queryString);

}
function handleStateChange(){
var responseDiv = document.getElementByIdx_x_x_x_x_x("serverResponse");
if(xmlHttp.readyState == 1){
responseDiv.innerHTML = "正在下载数据,请稍候<img src=loading.gif>";
if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
responseDiv.innerHTML = decodeURI(xmlHttp.responseText); //解码
}
}
</script>
</head><body>
<h2>输入姓名和生日</h2>
<form>
<input type="text" id="firstName" /><br>
<input type="text" id="birthday" />
</form>
<form>
<input type="button" value="GET" οnclick="doRequestUsingGET();" /><br>
<input type="button" value="POST" οnclick="doRequestUsingPOST();" />
</form>
<div id="serverResponse"></div>
</body>
</html>

建立ajax.php文件

PHP代码:


<?php

sleep(5);
echo

sleep(1);


?>



将两文件放apache的目录下,打开路径访问即可。小提醒:直接打开hmtl是不会执行的,必须路径访问


loading图片

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  PVcilKyJJTzb   2023年11月02日   49   0   0 nginxdockerhtml
xVpghvCvc9NK