天天看點

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>