所以我遇到了这个问题。我正在使用PhoneGap,并提出了一个基本的测验系统来解决问题。我工作得很好,我玩得很开心。它主要通过一个Web控制台工作,在那里我可以设置测验并循环查询问题,并对底层数据库进行一系列不太复杂的状态更新。我使用来自移动设备的Ajax调用来读取状态并显示问题或等待下一个。
基本电话是:
$(function checkForQuestion() {
var postData = "stuff needed in called PHP";
$.ajax({
type: "POST",
data: postData,
url: "my called php",
success: function(data){
var result = JSON.parse(data);
if (result == "question") {
--Display Question
} else {
setTimeout(checkForQuestion, 1000);
return false;
}
});
return false;
});
在我有限的经验中,如果需要等待新的状态,这似乎是Ajax的一般用法,带有递归回调。
现在这不是一切,而是我正在做的事情的品尝者。它非常适用于一个移动设备和一个Web控制台,通过测验很愉快。
当我尝试添加第二个移动设备(尚未尝试三个移动设备)时会发生什么事情,第二个移动设备只是默默地在它应该调用此ajax的地方失败。现在,第二个移动设备可以非常愉快地随意使用它,因此它不应该是硬件问题。在PHP中没有抛出任何错误,并且通过一些日志记录我无法找到任何明显失败的内容。
此时我想,也许我的网络主机上的连接存在限制(我只是使用我用来测试它的共享服务器网络主机)而且有,但这是Cloud Linux LVE限制为20个进入流程。但是,这不应该排除两个手机在大致相同的时间调用相同的PHP(通过wifi)。它应该只是同时运行调用,一个会比另一个快得多(或者这是我的理解)。
我想也许这是对MySQL数据库的锁定我不会允许多个用户/ PHP在大致相同的时间查询它,但PHP的连续性应该排除和谷歌的一些保证我呼叫将排队。
我猜测正在发生的事情是两个调用进来而没有返回任何错误,一个静默地与另一个导致失败,但我不知道找到失败并修复它。
有什么建议吗?
请求实际代码:
$(function () {
$(document).on("pagebeforeshow", "#page6", function () {
document.getElementById("mob_user_name2").innerHTML = window.localStorage.getItem("mob_local_login_name");
$(function checkForQuestion() {
//sort out the data to be posted
var postData = "mob_quizwait_quizcode=" . concat(window.localStorage.getItem("mob_local_quiz_code"), "&mob_quizwait_email=", window.localStorage.getItem("mob_local_login_email"), "&mob_quizwait_password=", window.localStorage.getItem("mob_local_login_password"), "&mob_quizwait_questionid=", window.localStorage.getItem("mob_question_id"));
$.ajax({
type: "POST",
data: postData,
url: "url of php",
success: function(data){
var mob_quizwait_data = JSON.parse(data);
if (mob_quizwait_data.mob_quizwait_success == "mob quizwait go") {
window.localStorage.setItem("mob_question_id", mob_quizwait_data.mob_quizwait_questionid);
window.localStorage.setItem("mob_question_score", mob_quizwait_data.mob_quizwait_score);
window.localStorage.setItem("mob_question_category", mob_quizwait_data.mob_quizwait_category);
window.localStorage.setItem("mob_question_question", mob_quizwait_data.mob_quizwait_question);
window.localStorage.setItem("mob_answer1", mob_quizwait_data.mob_quizwait_correct);
window.localStorage.setItem("mob_answer2", mob_quizwait_data.mob_quizwait_wrong1);
window.localStorage.setItem("mob_answer3", mob_quizwait_data.mob_quizwait_wrong2);
window.localStorage.setItem("mob_answer4", mob_quizwait_data.mob_quizwait_wrong3);
$.mobile.changePage("#page7", {transition:"slide", changeHash:false});
} else if (mob_quizwait_data.mob_quizwait_success == "mob quizwait stay") {
setTimeout(checkForQuestion, 1000);
return false;
} else if (mob_quizwait_data.mob_quizwait_success == "mob quizwait intermission") {
document.getElementById("mob_intermission").innerHTML = "Intermission";
$.mobile.changePage("#page6", {transition:"none", changeHash:false});
} else if (mob_quizwait_data.mob_quizwait_success == "mob quizwait finish") {
$.mobile.changePage("#page8", {transition:"slide", changeHash:false});
} else {
navigator.notification.alert("Status Check Failed. Please Try Again.", function(){}, "Alert", "OK");
}
}
});
return false;
});
});
});
PHP:
<?PHP
include '../open.php';
//take in POST variables
$quizcode = $link->real_escape_string($_POST["mob_quizwait_quizcode"]);
$email = $link->real_escape_string($_POST["mob_quizwait_email"]);
$password = $link->real_escape_string($_POST["mob_quizwait_password"]);
$questionid1 = $link->real_escape_string($_POST["mob_quizwait_questionid"]);
$quizid = 0;
$status = "X";
$questionid = 0;
$sql = "SELECT QUIZ_ID FROM B_QUIZ WHERE QUIZ_CODE = '$quizcode'";
$res = $link->query($sql);
while ($row = $res->fetch_array()) {
$quizid = $row['QUIZ_ID'];
}
$sql = "SELECT USER_ID FROM A_USER WHERE USER_EMAIL = '$email' AND USER_PASSWORD = '$password'";
$res = $link->query($sql);
while ($row = $res->fetch_array()) {
$userid = $row['USER_ID'];
}
$sql = "SELECT QUIZ_STATUS, IFNULL(QUESTION_ID, 0) AS QUESTION_ID FROM B_GAME WHERE QUIZ_ID = $quizid";
$res = $link->query($sql);
while ($row = $res->fetch_array()) {
$status = $row['QUIZ_STATUS'];
$questionid = $row['QUESTION_ID'];
}
if ($questionid == $questionid1) {
$questionid = 0;
}
if ($status != "F") {
if ($quizid != 0 && $status != "X") {
//get details to be written to the profile page
if ($questionid != 0) {
$sql = "SELECT SCORE FROM B_PARTICIPANT WHERE USER_ID = $userid AND QUIZ_ID = $quizid";
$res = $link->query($sql);
while ($row = $res->fetch_array()) {
$score = $row['SCORE'];
}
$sql = "SELECT b.QUESTION_ID, c.CATEGORY, b.QUESTION, b.CORRECT_ANSWER, b.WRONG_ANSWER_1, b.WRONG_ANSWER_2, b.WRONG_ANSWER_3
FROM B_GAME a, B_QUESTION b, D_CATEGORY c
WHERE a.QUESTION_ID = b.QUESTION_ID
AND b.CATEGORY_ID = c.CATEGORY_ID
AND a.QUIZ_ID = $quizid";
$res = $link->query($sql);
while ($row = $res->fetch_array()) {
$questionid = $row['QUESTION_ID'];
$category = $row['CATEGORY'];
$question = $row['QUESTION'];
$correct = $row['CORRECT_ANSWER'];
$wrong1 = $row['WRONG_ANSWER_1'];
$wrong2 = $row['WRONG_ANSWER_2'];
$wrong3 = $row['WRONG_ANSWER_3'];
}
if ($status == "R") {
$arr = array("mob_quizwait_success" => "mob quizwait go",
"mob_quizwait_questionid" => $questionid,
"mob_quizwait_score" => $score,
"mob_quizwait_category" => $category,
"mob_quizwait_question" => $question,
"mob_quizwait_correct" => $correct,
"mob_quizwait_wrong1" => $wrong1,
"mob_quizwait_wrong2" => $wrong2,
"mob_quizwait_wrong3" => $wrong3);
echo json_encode($arr);
} else if ($status == "N") {
$arr = array("mob_quizwait_success" => "mob quizwait stay");
echo json_encode($arr);
} else if ($status == "I") {
$arr = array("mob_quizwait_success" => "mob quizwait intermission");
echo json_encode($arr);
}
} else {
$arr = array("mob_quizwait_success" => "mob quizwait stay");
echo json_encode($arr);
}
} else {
$arr = array("mob_quizwait_success" => "mob quizwait failed");
echo json_encode($arr);
}
} else {
$arr = array("mob_quizwait_success" => "mob quizwait finished");
echo json_encode($arr);
}
include '../close.php';
?>
open.php:
<?PHP
//DATABASE DETAILS//
$DB_ADDRESS = "yeah";
$DB_USER = "not";
$DB_PASS = "gonna";
$DB_NAME = "do it";
//Connect to the MySQL database
$link = new mysqli($DB_ADDRESS, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect Failed: %s\n", mysqli_connect_error());
exit();
}
?>
close.php:
<?PHP
mysqli_close($link);
?>
答案 0 :(得分:2)
静默失败可能是因为处理ajax调用时出现服务器错误。
在ajax调用中添加error
处理程序。
$.ajax({
type: "POST",
data: postData,
url: 'url of php',
success: successFunction(),
error: function(xhr, status, err) { alert("Ajax request failed"); }
});
您可能需要单步执行PHP代码或添加其他日志记录以查看应用程序的流程。
答案 1 :(得分:0)
尝试:
<script>
var st_flag = true;
$(function () {
$(document).on("pagebeforeshow", "#page6", function () {
document.getElementById("mob_user_name2").innerHTML = window.localStorage.getItem("mob_local_login_name");
$(function checkForQuestion() {
if(!st_flag)
{
setTimeout(checkForQuestion, 1000);
}
//sort out the data to be posted
var postData = "mob_quizwait_quizcode=" . concat(window.localStorage.getItem("mob_local_quiz_code"), "&mob_quizwait_email=", window.localStorage.getItem("mob_local_login_email"), "&mob_quizwait_password=", window.localStorage.getItem("mob_local_login_password"), "&mob_quizwait_questionid=", window.localStorage.getItem("mob_question_id"));
$.ajax({
type: "POST",
data: postData,
url: "url of php",
st_flag = false;
success: function(data){
var mob_quizwait_data = JSON.parse(data);
if (mob_quizwait_data.mob_quizwait_success == "mob quizwait go") {
window.localStorage.setItem("mob_question_id", mob_quizwait_data.mob_quizwait_questionid);
window.localStorage.setItem("mob_question_score", mob_quizwait_data.mob_quizwait_score);
window.localStorage.setItem("mob_question_category", mob_quizwait_data.mob_quizwait_category);
window.localStorage.setItem("mob_question_question", mob_quizwait_data.mob_quizwait_question);
window.localStorage.setItem("mob_answer1", mob_quizwait_data.mob_quizwait_correct);
window.localStorage.setItem("mob_answer2", mob_quizwait_data.mob_quizwait_wrong1);
window.localStorage.setItem("mob_answer3", mob_quizwait_data.mob_quizwait_wrong2);
window.localStorage.setItem("mob_answer4", mob_quizwait_data.mob_quizwait_wrong3);
$.mobile.changePage("#page7", {transition:"slide", changeHash:false});
st_flag = true;
} else if (mob_quizwait_data.mob_quizwait_success == "mob quizwait stay") {
setTimeout(checkForQuestion, 1000);
return false;
} else if (mob_quizwait_data.mob_quizwait_success == "mob quizwait intermission") {
document.getElementById("mob_intermission").innerHTML = "Intermission";
$.mobile.changePage("#page6", {transition:"none", changeHash:false});
} else if (mob_quizwait_data.mob_quizwait_success == "mob quizwait finish") {
$.mobile.changePage("#page8", {transition:"slide", changeHash:false});
} else {
navigator.notification.alert("Status Check Failed. Please Try Again.", function(){}, "Alert", "OK");
}
}
});
return false;
});
});
});
</script>
等待第一个电话完成。
答案 2 :(得分:0)
即使你的问题本质上是复杂的,我也没有理由得出结论我会说,但我的赌注是session.auto_start。
正如我所见,你的代码中没有使用会话,在你的情况下,启动它可能是一个问题。将session.auto_start设置为false,这样您的代码就不会使用会话,然后测试您的代码。这是一个很长的镜头,但它是我得到的最好的。
为了解释自己,php默认使用文件进行会话。它通过以独占模式打开文件来实现。这意味着如果一个请求在服务器上运行,则第二个具有相同session_id的请求将被阻塞,直到第一个请求实现它。因此,每个浏览器一次只能在服务器上执行一个请求。我知道你的javascript在返回第一个请求之前不会发出第二个请求,但正如我所说,你的问题很复杂,这是我能想到的唯一猜测,我相信它值得一试。
所以我的解决方案是:确保您根本不使用会话。
答案 3 :(得分:0)
正如Kami所说,我认为服务器端(PHP)应该存在一些问题。很多时候,PHP不会用于显示由于服务器设置而导致的错误。我建议你检查apache错误日志,这样你就可以知道是否存在任何未显示的错误。要检查这一点,只需加载出现问题的URL,然后检查var/log/
文件夹中通常存在的错误日志。
答案 4 :(得分:0)
编辑后我有一个奇怪的建议。 Wifi热点通过NAT隐藏真实地址,因此从服务器的角度来看,您的设备都具有相同的IP地址。服务器将答案发送到您的Wifi点,并将数据包路由到实际地址。有时我丢失了包,但是当所有数据包丢失时我都记不清了。
您应该从两个设备请求页面并在服务器端记录scrpit的工作结果。如果有2个请求且所有数据库请求都正常工作,则意味着您的某个客户端未获得响应。这可能是因为你的路由器,可能是NAT
答案 5 :(得分:0)
对于Web Host提供商来说,这是一个未知问题。尽管有十几个详细的请求,他们一直在阻止事情我总是被提到我自己的设备或自己的代码是如何错。
为自己设置一个VPS并进行更改 - 我有一个无需修改代码或设备的工作系统。
TIL我不喜欢那些指责别人而掩盖其无能的人。