gpt4 book ai didi

ajax - 关于 Ajax 的问题

转载 作者:行者123 更新时间:2023-12-02 07:52:28 25 4
gpt4 key购买 nike

我目前正在学习 Ajax。下面的代码基本上接收来自 PHP 的回显,然后将其放入元素 id games 中。

我的问题是,如果我想让 Ajax 向 3 个不同的 PHP 脚本发送 3 个不同的 HTTP 请求,并且如果我想从每个脚本中检索数据,然后将其放入 3 个不同的元素 ID,那么我会制作 3 个副本吗相同的功能?我想应该有一种更有效的方法。

function showHint(str) {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("games").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","hw9.database.php?name="+str,true);
xmlhttp.send();
}

最佳答案

没有必要这样做。您只需要参数化函数:

function showHint(elementid,url,str) {

if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById(elementid).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",url+str,true);
xmlhttp.send();
}

关于ajax - 关于 Ajax 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2847037/

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