我是法国人,对我糟糕的英语很抱歉,我正在尝试为phoegap做一个测验
我有一个工作的ajax_actions.php
<?php
require('./PDOsql.class.php');
require('./Country.class.php');
require('./Question.class.php');
require('./Answer.class.php');
header('Access-Control-Allow-Origin: *');
if(isset($_POST["actionname"])&&!empty($_POST['actionname'])){
$actionname = $_POST['actionname'];
if($actionname=='getQuestion'){
$name = $_POST['name'];
$c = new Country();
$c->getIdByName($name);
$CountryId = $c->getId();
$q = new Question();
$a = new Answer();
$allQuestion = array();
$allQuestion = $q->getQuestionByCountryId($CountryId);
$allAnswer = array();
foreach ($allQuestion as $question) {
$allAnswer[] = array(
$a->getAnswerByQuestionId($question['id'])
);
}
die(
json_encode(
array(
'state'=>'success','allQuestion'=>$allQuestion,'allAnswer'=>$allAnswer
)
)
);
}
}
我有两节课: 问题:
<?php
class Question {
private $id="";
private $text="";
private $connect;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getText()
{
return $this->text;
}
public function setText($text)
{
$this->text = $text;
return $this;
}
public function getQuestionByCountryId($CountryId){
$this->connect = new PDOsql();
$sql='SELECT * FROM Question WHERE countryId = ? ORDER BY RAND() LIMIT 5';
$opt = $CountryId;
$value = $this->connect->query($sql,$opt);
$A_Question = array();
while($question = $value->fetch()){
$A_Question[] = array(
'id'=>$question['id'],
'text'=>$question['text']
);
}
$this->connect = null;
return $A_Question;
}
}
我有一个答案课:
<?php
class Answer {
private $id="";
private $text="";
private $isGood="";
private $connect;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getText()
{
return $this->text;
}
public function setText($text)
{
$this->text = $text;
return $this;
}
public function getIsGood()
{
return $this->isGood;
}
public function setIsGood($isGood)
{
$this->isGood = $isGood;
return $this;
}
public function getAnswerByQuestionId($QuestionId){
$this->connect = new PDOsql();
$sql='SELECT * FROM Answer WHERE QuestionId = ?';
$opt = $QuestionId;
$value = $this->connect->query($sql,$opt);
$A_Answer = array();
while($answer = $value->fetch()){
$A_Answer[] = array(
'id'=>$answer['id'],
'text'=>$answer['text'],
'isGood'=>$answer['isGood']
);
}
$this->connect = null;
return $A_Answer;
}
}
点击时点击js函数:
function getQuestion(name){
$.ajax({
type: "POST",
url: url,
dataType: "json",
data : {
actionname : 'getQuestion',
name:name
},
success: function(data) {
allQuestion = data.allQuestion;
allAnswer = data.allAnswer;
for (var i = allQuestion.length - 1; i >= 0; i--) {
alert(allQuestion[i]["text"]);
for (var j = 0; j < 4 ; j++) {
alert("answer "+j+" "+allAnswer[i][j]["text"]); //HERE THAT DOESNT WORKS !
};
};
},
error: function(data) {
alert("error !");
}
});
}
问题是,我不能得到我的答案清单:/ 谢谢你的帮助
答案 0 :(得分:1)
ajax中的A(异步JavaScript + XML)用于异步。这意味着,当ajax请求被触发时,不知道完成所需的时间。因此,在这种情况下,您无法确保订单。
什么是顺序是循环将迭代并触发多个ajax请求。但是,在每次迭代之间,很可能(尽管可能)请求将完成并触发成功处理程序。同样,异步编程不顺序编程。
请参阅Asynchronous vs synchronous execution, what does it really mean?
答案 1 :(得分:-1)
好的,所以我没有选择我需要把我的ajax 2放在我的ajax 1中:/