ajax发送请求案例,AJAX案例一:发送POST请求

function createXMLHttpRequest() {

try {

return new XMLHttpRequest();

} catch (e) {

try {

return ActvieXObject("Msxml2.XMLHTTP");

} catch (e) {

try {

return ActvieXObject("Microsoft.XMLHTTP");

} catch (e) {

alert("用的是什么浏览器啊?");

throw e;

}

}

}

}

window.onload = function() {

var btn = document.getElementById("btn");

btn.onclick = function() {

var xmlHttp = createXMLHttpRequest();

//修改为POST请求方式

xmlHttp.open("POST", "", true);

//POST请求要设置请求头

xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

//添加参数

xmlHttp.send("username=张三&password=123

你可能感兴趣的:(ajax发送请求案例)