天天看点

ajax—post请求

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

</head>

<body>

     <div class="show"></div>

</body>

<script type="text/javascript">

//get请求和post请求的差别 

//1、post请求需要设置请求头 get不需要

//2、get请求有缓存问题 需要在网址后拼接随机数 post1请求不需要

//3、get请求传递的数据拼接在网址后面 post请求写在send里

var showDiv=document.getElementsByClassName('show')[0];

//创建ajax对象

var ajax=new XMLHttpRequest();

ajax.οnlοad=function(){

var data=ajax.responseText;

      showDiv.innerHTML=data;

}

//发送请求

ajax.open('POST','http://localhost/AJAX/php/post.php',true);

//请求头 post请求需要设置请求头 位置必须在open和send之间

ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

ajax.send('name=西安宋小宝&age=23');

</script>

</html>