1:get请求方式:浏览器
// 1:建立XMLHttpRequest对象 var xhr; if (window.XMLHttpRequest) { // 其余类型的浏览器 xhr = new XMLHttpRequest(); } else { // ie浏览器 xhr = new ActiveXObject("Microsoft.XMLHTTP"); } // 2:配置请求信息 xhr.open('get', 'ProcessShow.ashx', true); // 3:发送请求 xhr.send(); // 4:监听状态 注册onreadystatechange事件 xhr.onreadystatechange = function() { // 5:判断请求和相应是否成功 if (xhr.readyState == 4 && xhr.status == 200) { // 6:获取数据 并作相应的处理 var data = xhr.responseText; } }
这是一个完整的Ajax的get请求步骤。ide
若是get请求须要传递数据,就这样写:post
xhr.open('get', 'ProcessShow.ashx?id=1&name=rose', true);
2:post请求方式:对象
// 1:建立XMLHttpRequest对象 var xhr; if (window.XMLHttpRequest) { // 其余类型的浏览器 xhr = new XMLHttpRequest(); } else { // ie浏览器 xhr = new ActiveXObject("Microsoft.XMLHTTP"); } // 2:配置请求信息 xhr.open('post', 'ProcessShow.ashx', true); // 3:发送请求 xhr.send(); // 为空表示没有任何的参数 // 4:监听状态 注册onreadystatechange事件 xhr.onreadystatechange = function() { // 5:判断请求和相应是否成功 if (xhr.readyState == 4 && xhr.status == 200) { // 6:获取数据 并作相应的处理 var data = xhr.responseText; } }