1.创建XMLHttpRequest对象。
2.向servlet发送请求,post,get方式,其中post方式
2.向servlet发送请求,post,get方式,其中post方式
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
这句话要写到发送之前。
3、发送请求和接受反馈只能由javascript来处理,因此js处理的只能是字符串,不能接收一个对象或者数据集。
var xmlHttp = null;
//建立XMLHttpRequest对象
function createHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
if(xmlHttp == null)
{
//异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest!");
return;
}
xmlHttp.onreadystatechange = dealResult;
}
//发送请求
function sendRequest(url, postStr)
{
createHttpRequest();
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(postStr);
}
//发送请求
function sendGetRequest(url)
{
createHttpRequest();
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}