ajax的原生写法

来源:互联网 发布:淘宝v6会员帐号值钱吗 编辑:程序博客网 时间:2024/06/05 06:34
原生的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" onclick="n_ajax()">按钮</button></body><script>//原生的ajaxvar 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>

原创粉丝点击