名字的来历:
AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)
优点是 在不刷新整个浏览器的情况下,与服务器交换数据并更新部分网页内容
AJAX是基于现有的Internet标准
AJAX是基于现有的Internet标准,并且联合使用它们:
- XMLHttpRequest 对象 (异步的与服务器交换数据)
- JavaScript/DOM (信息显示/交互)
- CSS (给数据定义样式)
- XML (作为转换数据的格式)
AJAX应用程序与浏览器和平台无关的!
创建实例
function request() {
var xmlhttp;
// 适配IE4 IE5
if (window.XMLHttpRequest) {
// 满足大部分浏览器
xmlhttp = new XMLHttpRequest();
} else {
// IE4 IE5 专用
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText)
}
}
/*
GET
*/
xmlhttp.open("GET", "/try/ajax/ajax?a=3&b=4", true);
xmlhttp.send();
/*
POST
xmlhttp.open("POST", "/try/ajax/demo_post2.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
*/
}