原生的ajax的来操作html标签的变化
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ajax</title>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<p id="p">我是段落</p>
<button type="" id="but" οnclick="n_ajax()">按钮</button>
</body>
<script>
//原生的ajax
var p = document.getElementById('p');
var xhr = new XMLHttpRequest();
function n_ajax(){
var data = 'name=hello world!';
xhr.open("POST", "./a.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // 添加http头,发送信息至服务器时内容编码类型
xhr.send(data);
xhr.onreadystatechange = function(){
alert(xhr.readyState);
if(xhr.readyState == '4' ){
if(xhr.status == '200'){
var ppt = p.innerHTML += xhr.responseText;
}
};
};
}
</script>
</html>