详细:https://www.cnblogs.com/wupeiqi/articles/5703697.html
ajax
1.不刷新页面,向后台发送数据
2.Ajax主要就是使用 【XmlHttpRequest】对象来完成请求的操作
3.原生ajax:
GET请求:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
alert(xhr.responseText);
}
};
xhr.open('GET','/add2/?i1=12&i2=19');
xhr.send();
POST请求:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
alert(xhr.responseText);
}
};
xhr.open('POST','/add2/');
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send("i1=12&i2=19");
4.伪Ajax,非XMLHttpRequest
技术:
iframe标签,不刷新发送HTTP请求
<form>....</form>
iframe的name必须跟form的target一样才可以建立关联。
示例:
<form id="f1" method="POST" action="/fake_ajax/" target="ifr">
<iframe id="ifr" name="ifr" style="display: none"></iframe>
<input type="text" name="user" />
<a onclick="submitForm();">提交</a>
</form>
<script>
function submitForm(){
document.getElementById('ifr').onload = loadIframe;
document.getElementById('f1').submit();
}
function loadIframe(){
var content = document.getElementById('ifr').contentWindow.document.body.innerText;
alert(content);
}
</script>