<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title></title> <script type="text/javascript" src="AjaxUtil.js"></script> </head> <body> <script type="text/javascript"> var url = ‘http://192.168.6.4:8082/WXInsuranceClaim/Detail‘; //AjaxUtil.addURLParam(url, ‘id‘, 920); //AjaxUtil.addURLParam(url, ‘t‘, Math.random()); var xhr = AjaxUtil.createXHR(); xhr.onreadystatechange = function () { if(xhr.readyState == 4) { if((xhr.status >=200 && xhr.status< 300) || xhr.status == 304) { alert(xhr.responseText); } else { alert(‘请求失败!‘ + xhr.status); } } } xhr.open(‘post‘, url, true); xhr.setRequestHeader(‘Content-Type‘, ‘application/x-www-form-urlencoded‘); //xhr.open(‘get‘, url, true); xhr.send("id=920&t=" + Math.random()); </script> </body> </html>
var AjaxUtil = { //创建XMLHttpRequest对象,兼容IE7以下版本 createXHR: function () { if (typeof XMLHttpRequest != ‘undefined‘) { return new XMLHttpRequest(); } else if ( typeof ActiveObject != ‘undefined‘) { if (typeof arguments.callee.activeString != ‘string‘) { var versions = [‘MSXML.XMLHttp.6.0‘,‘MSXML.XMLHttp.3.0‘,‘MSXML.XMLHttp‘]; var i,len; for (i=0,len=versions.length; i<len; i++) { try { new ActiveObject(versions[i]); arguments.callee.activeString = versions[i]; break; } catch (ex) { } } return new ActiveObject(arguments.callee.activeString); } } else { throw new Error("浏览器不支持!"); } } //为GET请求添加参数 , addURLParam: function (url, name, value) { url += (url.indexOf(‘?‘) == -1) ? ‘?‘ : ‘&‘; url += encodeURIComponent(name) + ‘=‘ + encodeURIComponent(value); return url; } }
原文地址:http://ccdebug.blog.51cto.com/4350056/1430306