1 .无刷新留言
<script type="text/javascript">
function getIdObj(id) { return document.getElementById(id); }
window.onload = function () {
document.getElementById(‘btnSumbit‘).onclick = function () {
var _Id = document.getElementById(‘txtId‘).value;
var _Class = document.getElementById(‘txtClass‘).value;
var str = ‘id=‘ + _Id + ‘&className=‘ + _Class;
var xhr = null;
if (typeof XMLHttpRequest != ‘undefined‘) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject("Microsoft.XMLHttp");
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var res = xhr.responseText;
var msgObj=document.getElementById(‘msgId‘);
if (res == 1)
{
msgObj.innerHTML = "提交成功!";
msgObj.style.color = "green";
var tboduObj = document.getElementById(‘tdid‘).getElementsByTagName(‘tbody‘)[0];
var trObj = tboduObj.insertRow(-1);
trObj.insertCell(-1).innerHTML = 1;
trObj.insertCell(-1).innerHTML = _Class;
trObj.insertCell(-1).innerHTML = _Id;
}
}
};
xhr.open(‘post‘, ‘AddClass.ashx‘, true);
xhr.setRequestHeader(‘Content-Type‘, ‘application/x-www-form-urlencoded‘);
xhr.send(str);
};
};
</script>
2.JsHelper
var JsHelper = {
getXHR: function () {
var xhr = null;
if (typeof XMLHttpRequest != ‘undefined‘) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject(‘Microsoft.XMLHttp‘);
}
return xhr;
},
doAjax: function (method, url, data, isAyn, callback) {
method=method.toLowerCase();
var xhr = this.getXHR();
xhr.open(method,url+(method=="get"?‘?‘+data:‘‘), isAyn);
if (method == ‘get‘) {
xhr.setRequestHeader(‘If-Modified-Since‘, 0);
} else {
xhr.setRequestHeader(‘Content-Type‘,‘application/x-www-form-urlencoded‘);
}
xhr.onreadystatechange = function () {
callback(xhr);
};
xhr.send(method != "get" ? data : null);
}
};