window.onload = function(){
document.getElementById("ok").onclick = function(){
var xhr = ajaxFunction();
xhr.open("POST", "../servlet/testServlet?timeStamp=" + new Date().getTime(), true);
//如果请求类型是POST的话,需要设置请求头部信息Content-Type
//application/x-www-form-urlencoded
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("a=7&b=8");
xhr.onreadystatechange = function(){
if (xhr.readyState == 4) {
alert(xhr.status);
if (xhr.status == 200 || xhr.status == 304) {
alert("tfy");
var data=xhr.responseText;
alert(data);
}
}
}
}
}
document.getElementById("ok").onclick = function(){
var xhr = ajaxFunction();
xhr.open("POST", "../servlet/testServlet?timeStamp=" + new Date().getTime(), true);
//如果请求类型是POST的话,需要设置请求头部信息Content-Type
//application/x-www-form-urlencoded
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("a=7&b=8");
xhr.onreadystatechange = function(){
if (xhr.readyState == 4) {
alert(xhr.status);
if (xhr.status == 200 || xhr.status == 304) {
alert("tfy");
var data=xhr.responseText;
alert(data);
}
}
}
}
}
本文介绍了一个使用JavaScript的Ajax技术发送POST请求的例子。通过这个示例,读者可以了解到如何设置请求头、发送参数以及处理服务器响应的状态和返回的数据。
8977

被折叠的 条评论
为什么被折叠?



