[英]Second call out of two xmlhttprequest doesn't work
I've read a lot of how to try and make two xmlhttprequest in parallel, but it looks like something doesn't quite work. 我已经读了很多关于如何尝试并行创建两个xmlhttprequest的方法,但是看起来有些工作不太正常。
I have 1 php file. 我有1个php文件。 which includes 2 .js files. 其中包括2个.js文件。
The first runs xmlhttprequest
every 3 seconds. 第一个每3秒运行xmlhttprequest
。
I want the second to run on demand, but whenever i trigger it, it returns with status 4 but the responseText
is always empty. 我希望第二个按需运行,但是每当我触发它时,它就会返回状态4,但responseText
始终为空。 (the PHP file prints with no question, i even tried to put on the PHP file just window.open('1')
to see that the file is called and its not). (毫无疑问,PHP文件可以打印,我什至试图将PHP文件放在window.open('1')
以查看该文件被调用而没有被调用)。
Here is the first JS : 这是第一个JS:
var req1 = createXMLHttpRequest2();
var user_redirected = false; var user_redirected = false;
function createXMLHttpRequest2() {
var ua2;
if(window.XMLHttpRequest) {
try {
ua2 = new XMLHttpRequest();
} catch(e) {
ua2 = false;
}
} else if(window.ActiveXObject) {
try {
ua2 = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
ua2 = false;
}
}
return ua2;
}
function set_user_redirected_false() {
user_redirected = false;
}
function get_user_redirected() {
return user_redirected;
}
function handleResponse(username, game_id, isInvitation) {
if(req1.readyState == 4 && req1.status==200) {
var response = req1.responseText;
if (response == "true") {
// Ask to set the game_accepted var to 1 (user is redirected and not leaving)
user_redirected = true;
if (isInvitation == "true") {
window.location.href = "game.php?game_id="+game_id+"&position=2";
} else {
window.location.href = "game.php?game_id="+game_id+"&position=1";
}
}
else {
setTimeout(function(){sendRequest();}, 3000);
}
}
}
function sendRequest() {
user_redirected = false;
var username = "";
var game_id = -1;
var isInvitation = "false";
username = document.getElementById("username").value;
game_id = document.getElementById("game_id").value;
isInvitation = document.getElementById("invitation").value;
if (isInvitation == "true") {
req1.open('GET', 'check_for_inviter.php?username='+username+'&game_id='+game_id ,true);
} else {
req1.open('GET', 'check_for_opponent.php?username='+username+'&game_id='+game_id,true);
}
req1.onreadystatechange = function(){handleResponse(username, game_id, isInvitation);};
req1.send(null);
}
This is the second JS file : 这是第二个JS文件:
function createXMLHttpRequest() {
var ua;
if(window.XMLHttpRequest) {
try {
ua = new XMLHttpRequest();
} catch(e) {
ua = false;
}
} else if(window.ActiveXObject) {
try {
ua = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
ua = false;
}
}
return ua;
}
function delete_waiting_games(username) {
var req2 = createXMLHttpRequest();
req2.open('GET', 'delete_waiting_games_for_username.php');
req2.onreadystatechange = function(){
window.open(req2.readyState+'&'+req2.responseText);
};
req2.send(null);
}
As you can see i open a new window to see the response and the ready state (just for testing) and i always get status 4 and empty responseText. 如您所见,我打开一个新窗口以查看响应和就绪状态(仅用于测试),并且我始终获得状态4和空的responseText。
Thanks. 谢谢。
Use setTimeout
to separate the calls, and with
to encapsulate the XMLHTTPRequest
: 使用setTimeout
来调用划分,并with
封装XMLHTTPRequest
:
function xhr()
{
with(new XMLHttpRequest)
{
open("GET",{},true);
setRequestHeader("Foo", "Bar");
send("");
onreadystatechange = handler;
}
}
function handler(event)
{
!!event.target && !!event.target.readyState && event.target.readyState === 4 && ( console.log(event) );
}
setTimeout(xhr, 500);
setTimeout(xhr, 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.