gpt4 book ai didi

javascript - XMLHttpRequest 输出未显示

转载 作者:行者123 更新时间:2023-12-03 11:45:11 24 4
gpt4 key购买 nike

这是我的 JavaScript 函数:-

已解决的 JavaScript 函数:-

function delfile(fileid) {
var div = "fileid_" + fileid;
var location = "/delfile/" + fileid;
if (window.XMLHttpRequest)
{
var xmlhttp=new XMLHttpRequest();
}
else
{
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(div).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST", location, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("");
}

这是我从 PHP 生成的内容:-

echo "<td><div id='fileid_{$id}'><button onclick='delfile({$id})'>Delete File</button></div></td>";

我的问题是,当用户同时单击 2-3 个链接时,http 请求将通过 javascript 发送,并且文件会在服务器上删除,但 div id 不会随 http 请求的输出而更新。只有用户最后单击的按钮才会获取 http 请求的输出。

PHP脚本绝对没有问题,我只是给大家引用一下。 delfile 脚本仅发送“File Delted”。

按照@torazaburo的建议,通过使用局部变量来解决。

最佳答案

您未能将 xmlhttp 声明为该函数的本地函数,这意味着它在多个请求之间共享,这会导致灾难。

同时,请同时声明该函数的 $div$location 。另外,请删除这些变量名称的 $ 前缀,因为众所周知,这种做法会腐 eclipse 大脑。

如果您输入了“use strict;”,您未能声明变量的情况就会被记录下来。正如您应该的那样,位于函数的顶部。

关于javascript - XMLHttpRequest 输出未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com