[英]Call multiple functions from AJAX and return separate data
I'm trying to get two distinct results from AJAX calls. 我正在尝试从AJAX调用中获得两个不同的结果。 I pass in
_functionToRun
to the PHP file, which uses a switch statement to determine which function to run. 我将
_functionToRun
传递给PHP文件,该文件使用switch语句确定要运行的函数。
In the PHP file, I echo out two different arrays of words... newQuestWords()
should only return some of the words. 在PHP文件中,我回显了两个不同的单词数组...
newQuestWords()
应该只返回某些单词。 parseHoveredText()
should return all vocab in the database. parseHoveredText()
应该返回数据库中的所有vocab。
Yet it seems when I echo out both, just all of the vocab is returned... 然而,当我回声两者时,似乎只有所有唱主角都被送回了...
For example, in the JavaScript, log(hoveredWords[i]);
例如,在JavaScript中,
log(hoveredWords[i]);
should show all of the vocab available in the database, as returned from `parseHoveredText()
function, but it doesn't show anything. 应该显示从
`parseHoveredText()
函数返回的数据库中所有可用的vocab,但不显示任何内容。
Why is that? 这是为什么?
JavaScript: JavaScript的:
$(document).ready(function() {
//initially append words to word bank
$.getJSON("php/Quests.php", { "_questNum" : questNum, "_functionToRun" : 1},
function(returned_data) {
wordsArray = returned_data;
$.each(wordsArray, function(key, value) {
$(".wordBank_Words").append("<span class='bankword' data-display-word='" + key + "' ><b>" + key + "</b>: " + value + "</span><br/>");
}
);
});
//get all chinese/english words for hover over translation
$.getJSON("php/Quests.php", {"_functionToRun" : 2},
function(returned_data) {
hoveredWords = returned_data;
for (var i = 0; i < hoveredWords.length; i++) {
log(hoveredWords[i]);
}
});
PHP: PHP:
<?php
//if user's input is correct, increment task number, get next vocabulary
$functionToRun = (isset($_GET['_functionToRun'])) ? $_GET['_functionToRun'] : 1;
parseHoveredText();
switch ($functionToRun)
{
case 1:
newQuestWords();
break;
case 2:
parseHoveredText();
break;
default:
echo "defaulted...";
}
function newQuestWords () {
include 'DbConnect.php';
$questNumber = (isset($_GET['_questNum'])) ? $_GET['_questNum'] : 1;
$qry =
"SELECT t.*, v.*
FROM task t
INNER JOIN vocabtask vt ON (t.id = vt.taskid)
INNER JOIN vocab v ON (v.id = vt.vocabid)
WHERE vt.taskid = " . $questNumber;
$sql = $mysqli->query($qry);
$wordsArray = array();
while ($row = $sql->fetch_assoc()) {
$wordsArray[$row['chinese']] = $row['english'];
}
mysqli_close($mysqli);
echo json_encode($wordsArray);
}
function parseHoveredText () {
include 'DbConnect.php';
$qry =
"SELECT v.*
FROM vocab v";
$sql = $mysqli->query($qry);
$hoveredWords = array();
while ($row = $sql->fetch_assoc()) {
$hoveredWords[$row['chinese']] = $row['english'];
}
mysqli_close($mysqli);
//return Chinese and English Words
echo json_encode($hoveredWords);
}
?>
Dump return_data, it is json object not an array you can iterate. 转储return_data,它是json对象,不是可以迭代的数组。 You can iterate through it this way
您可以通过这种方式进行迭代
...
function(returned_data) {
for (word in returned_data) {
console.log(word);
}
}
...
Or with $.each 或与$ .each
...
$.each(returned_data, function(key, value) {
console.log(value);
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.