是否可以同时向两个或多个Php脚本发送Ajax请求?我知道这可以连续实现(从1获得响应然后从另一个获得响应)但我想知道它是否可能同时实现。请帮我处理以下代码:
function calShowUpload(){
if (http.readyState == 4) {
res = http.responseText;
document.getElementById('maincontent').innerHTML=res;
}
}
function showUpload(obj)
{
http.open("GET", "./showUpload.php", true);
http.onreadystatechange = calShowUpload;
http.send(null);
}
function getXHTTP() {
var xhttp;
try { // The following "try" blocks get the XMLHTTP object for various browsers…
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2) {
// This block handles Mozilla/Firefox browsers...
try {
xhttp = new XMLHttpRequest();
}
catch (e3) {
xhttp = false;
}
}
}
return xhttp; // Return the XMLHTTP object
}
var http = getXHTTP(); // This executes when the page first loads.
在上面的示例中,我知道我可以在showUpload(obj)
内调用另一个函数calShowUpload()
来实现我的目标,但我需要这样的事情:
function showUpload(obj)
{
http.open("GET", "./showUpload.php", true);
http.open("GET", "./showDelete.php", true);
http.onreadystatechange = calShowUpload;
http.send(null);
}
答案 0 :(得分:5)
您需要XMLHttpRequest的两个实例,否则第二个将踩踏第一个实例。使用现有代码执行此操作的最简单方法是创建两个实例并编写JS以使用适合该任务的任何一个实例。
var http1 = getXHTTP(); // Use this for one request.
var http2 = getXHTTP(); // Use this for the other request.