改写下面的代码,要求结果一致:function addHandleHomework(username,table,destination) { createXMLHttpRequest(); //调用创建XMLHttpRequest对象的方法 xmlHttp.onreadystatechange=callback; //设置回调函数 var myDate = new Date(); var nowtime=myDate.Format("yyyy-MM-dd hh:mm:ss"); var idtime=myDate.Format("yyyyMMddhhmmss"); var HandleHomeworkId=idtime; var StudentId=username; var HomeworkId = document.getElementById("HomeworkId").value; var Title = document.getElementById("Title").value; var Title_code = encodeURI(encodeURI(Title)); var Content = document.getElementById("Content").value; var Content_code = encodeURI(encodeURI(Content)); var HandleHomeworkCreateTime = nowtime; var url="insertonehandlehomework?table="+table+"&HandleHomeworkId="+HandleHomeworkId+"&HomeworkId="+HomeworkId+"&StudentId="+StudentId+"&Title="+Title_code+"&Content="+Content_code+"&HandleHomeworkCreateTime="+HandleHomeworkCreateTime; alert(url); xmlHttp.open("post",url,true); //向服务器端发送请求 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8"); xmlHttp.send("HomeworkId="+HomeworkId); function callback() { if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { var data= xmlHttp.responseText; data=data.toString(); if(data=="successful") { alert("创建成功"); window.location.href=destination; } else if(data=="failed") { alert("创建失败,请重试"); location.reload(); } } } } }
时间: 2024-04-03 11:37:27 浏览: 81
function addHandleHomework(username, table, destination) {
createXMLHttpRequest(); // 调用创建XMLHttpRequest对象的方法
xmlHttp.onreadystatechange = callback; // 设置回调函数
const nowtime = new Date().toISOString().slice(0, 19).replace('T', ' '); // 获取当前时间
const idtime = nowtime.replace(/\D/g, ''); // 去除时间中的非数字字符
const HandleHomeworkId = idtime; // 生成处理作业的ID
const StudentId = username;
const HomeworkId = document.getElementById("HomeworkId").value;
const Title = document.getElementById("Title").value;
const Title_code = encodeURIComponent(Title); // 编码标题
const Content = document.getElementById("Content").value;
const Content_code = encodeURIComponent(Content); // 编码内容
const HandleHomeworkCreateTime = nowtime;
const url = `insertonehandlehomework?table=${table}&HandleHomeworkId=${HandleHomeworkId}&HomeworkId=${HomeworkId}&StudentId=${StudentId}&Title=${Title_code}&Content=${Content_code}&HandleHomeworkCreateTime=${HandleHomeworkCreateTime}`;
alert(url);
xmlHttp.open("POST", url, true); // 向服务器端发送请求
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");
xmlHttp.send(`HomeworkId=${HomeworkId}`);
function callback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
const data = xmlHttp.responseText;
if (data === "successful") {
alert("创建成功");
window.location.href = destination;
} else if (data === "failed") {
alert("创建失败,请重试");
location.reload();
}
}
}
}
}
阅读全文