javascript原生ajax;

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
//a、new对象;
var xmlhttpRequest =null;
function ajaxRequest(){
if(window.ActiveXObject){
xmlhttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else if(Window.XMLHttpRequest){
xmlhttpRequest = new XMLHttpRequest();
};
if(null != xmlhttpRequest){
var v1 = 1,v2 = 2;
//AjaxServlet其实是url,ajax数据存放的地址;
//b.连接服务器吧;
xmlhttpRequest.open("get","AjaxServlet?v1="+v1+"&v2="+v2,true);
xmlhttpRequest.open("POST","AjaxServlet",true);
//d、处理回调
xmlhttpRequest.onreadystatechange = ajaxCallBack;
//post必须加下面这句,get时去掉;
//c、设置响应头,看情况,上去
xmlhttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//d、数据发送
xmlhttpRequest.send("v1="+v1+"&v2="+v2);
}
}
function ajaxCallBack(){
if(xmlhttpRequest.readyState == 4){
if(xmlhttpRequest.status == 200){
var content = xmlhttpRequest.responseText;
console.log(content);
}
}
}
</script>
</body>
</html>
上一篇:Android用户界面布局(layouts)


下一篇:Java线程面试题 Top 50 (转载)