繁体   English   中英

几秒钟后更改文本http.responseText,.innerHTML(JavaScript,Ajax)

[英]Text changing after a few seconds http.responseText, .innerHTML (Javascript, Ajax)

当用户尝试从通过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);

}

文本更改,但几秒钟后消失。

在不了解其余代码的情况下,它可能与您拥有的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(); 

        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM