javascript实现增删改查
发布网友
发布时间:2022-04-29 04:44
我来回答
共1个回答
热心网友
时间:2022-04-22 14:28
使用XMLHttpRequest
下面给一共例子
function commitEdit(type, val, id) {
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
var sendData = "val=" + val + "&type=" + type + "&id=" + id;
http_request.onreadystatechange = function() {alertContents(http_request, type);};
url = "listUpdate.jsp";
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-Length", sendData.length);
http_request.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
http_request.send(sendData);
}
function alertContents(http_request, type) {
if (http_request.readyState == 4) {
var resTxt = trimString1(http_request.responseText);
if (resTxt == "-1") {
alert("内容不健康,添加错误!");
return;
}
if (type == "label") {
document.getElementById("listLabel").innerHTML = resTxt;
document.getElementById("addLabelLink").innerHTML = "修改标签";
} else if (type == "addLabel") {
document.getElementById("listLabel").innerHTML = resTxt;
document.getElementById("listLabelTr").style.display = 'block';
document.getElementById("addLabelLink").innerHTML = "修改标签";
document.getElementById("addLabelLink").href = "javascript:chngLabel();";
} else if (type == "listComment") {
document.getElementById("listReviewContext").innerHTML = resTxt;
} else if (type == "addFavorite") {
alert(resTxt);
}
}
}