几秒钟后更改文本http.responseText、.innerHTML(Javascript、Ajax)

几秒钟后更改文本http.responseText、.innerHTML(Javascript、Ajax),javascript,ajax,Javascript,Ajax,当用户试图从通过HTML网站显示的MYSQL数据库中删除包含产品的类别时,我使用这段代码向用户显示一条消息: function deleteCategory() { var http = new XMLHttpRequest(); var url = "/637415/admin/scripts/deleteCategory.php"; var params = "selectCatDelete=" + document.getElementById("selectedCatDelete")

当用户试图从通过HTML网站显示的MYSQL数据库中删除包含产品的类别时,我使用这段代码向用户显示一条消息:

function  deleteCategory()
{
var http = new XMLHttpRequest();
var url = "/637415/admin/scripts/deleteCategory.php"; 
var params = "selectCatDelete=" + document.getElementById("selectedCatDelete").value;
console.log("The value is:",  params);
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.responseText == 0 && http.status == 200) {
        document.getElementById("responseText").innerHTML = "You need to delete products from the category, go to the delete products page here: <a href='/637415/admin/deleteProduct.php'>Delete produy</a>";
    }
    if(http.readyState == 4 && http.status == 200) {
        document.getElementById("responseText").innerHTML = "Category deleted";
    }

}
http.send(params);
console.log(params);
函数deleteCategory()
{
var http=new XMLHttpRequest();
var url=“/637415/admin/scripts/deleteCategory.php”;
var params=“selectCatDelete=“+document.getElementById(“selectedCatDelete”).value;
log(“值为:”,参数);
http.open(“POST”,url,true);
//随请求一起发送正确的标头信息
http.setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
setRequestHeader(“内容长度”,参数长度);
setRequestHeader(“连接”,“关闭”);
http.onreadystatechange=function(){//在状态更改时调用函数。
if(http.responseText==0&&http.status==200){
document.getElementById(“responseText”).innerHTML=“需要从类别中删除产品,请转到此处的删除产品页面:”;
}
如果(http.readyState==4&&http.status==200){
document.getElementById(“responseText”).innerHTML=“类别已删除”;
}
}
http.send(params);
控制台日志(params);
}


文本会发生变化,但几秒钟后就会消失

在不知道代码其余部分的情况下,它可能与您拥有的location.reload()相关。一旦重新加载页面,您使用javascript所做的任何更改都将丢失,包括您的消息。

为什么不在请求完成后显示消息。看看这是否有效:

http.onreadystatechange = function() {//Call a function when the state changes.
   //if there is a request not initialized error:

    //Request finished and response is ready
    if(http.readyState == 4 && http.status == 200) {
      if(http.responseText == 0 ) {
        document.getElementById("responseText").innerHTML = "You need to delete 
        products from the category first, go to the delete products page here: 
        <a href='/637415/admin/deleteProduct.php'>Delete Products</a>";
        }else

        location.reload(); 

        }
}
http.onreadystatechange=function(){//在状态更改时调用函数。
//如果存在请求未初始化错误:
//请求已完成,响应已准备就绪
如果(http.readyState==4&&http.status==200){
如果(http.responseText==0){
document.getElementById(“responseText”).innerHTML=“您需要删除
类别中的产品首先转到此处的“删除产品”页面:
";
}否则
location.reload();
}
}

我添加了更多代码。我已删除位置。重新加载()。我现在的问题是无法验证来自PHP服务器的响应。如果MYSQL查询无法执行,我想显示消息。如果PHP代码中的SQL查询失败,您可以返回一个包含错误消息的字符串,也可以返回-1。我得到ReferenceError:deleteCategory在尝试这段代码时未定义。如果有帮助的话,我已经在最初的帖子中添加了更多的代码。