Ajax以post方式提交数据

前端之家收集整理的这篇文章主要介绍了Ajax以post方式提交数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JS

<script type="text/javascript">

function login() {

var username = document.getElementById("txtUsername").value;
var password = document.getElementById("txtPassword").value;

var url = "Login.aspx?op=1";
var postStr = "username="+ username +"&password="+ password;



var ajax = null;
if (window.XMLHttpRequest) {
ajax = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
return;
}

ajax.open("POST",url,true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send(postStr);


ajax.onreadystatechange = function() {
if (ajax.readyState == 4 && ajax.status == 200) {

document.getElementById("content").innerHTML = ajax.responseText; ;
}
}
}

</script>

html:

<div> 用户名: <input type="text" id="txtUsername" /> 密码: <input type="password" id="txtPassword" /> <input type="button" id="btnOk" value="确定" onclick="login()"/> <div id="content"></div> </div>

猜你在找的Ajax相关文章