来自Google Closure编译文件的TypeError

漂亮的

我有一个使用Google Closure编译器编译的Javascript文件,该错误提示我TypeError: f is undefined当我查看编译后的代码时,无法理解,但其中的大部分被注释掉了。我真的很困惑为什么会收到此错误,但我怀疑它与以下脚本有关(这是自收到此错误以来我唯一编辑的内容)。

var response;

var request = new goog.net.XhrIo();

goog.events.listen(request, "complete", function(){

    if (request.isSuccess()) {

        response = request.getResponseText();

        console.log("Satus code: ", request.getStatus(), " - ", request.getStatusText());

    } else {

        console.log(
        "Something went wrong in the ajax call. Error code: ", request.getLastErrorCode(),
        " - message: ", request.getLastError()
        );
    }

});


request.send("load_vocab.php");


var rawVocab = response[rawVocab];
var optionVocab = response[optionVocab];
alert(rawVocab.length);
alert(optionVocab.length);

这也是load_vocab.php ...

try {
    $conn = new PDO('mysql:host=localhost;dbname=tygrif_school', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $conn->prepare('SELECT word, translation, example_sentence_1 FROM vocabulary_game WHERE game_type = :game_type');
    $stmt->execute(array('game_type' => 'target'));

    while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
       $data['rawVocab'][] = $row;
    }

    $stmt = $conn->prepare('SELECT word, translation FROM vocabulary_game');
    $stmt->execute(array());

    while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
        $data['optionVocab'][] = $row;
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

echo json_encode($data);
HMR

您知道xhr请求是异步的吗?

这意味着当您调用send时,您必须等待响应返回,您要做的就是尝试在下一行读取响应。您的引用也有问题,编译器将重命名rawVocab和optionVocab,但不会重命名返回的数据,因此您需要引用ne8il指出的这些值。

var response;
var request = new goog.net.XhrIo();
goog.events.listen(request, "complete", function(){
    if (request.isSuccess()) {
        window['console'].log("Now the response returned, setting response variable");
        response = request.getResponseText();
        console.log("Satus code: ", request.getStatus(), " - ", request.getStatusText());
    } else {
        console.log(
        "Something went wrong in the ajax call. Error code: ", request.getLastErrorCode(),
        " - message: ", request.getLastError()
        );
    }
});
window['console'].log("Sending request");
request.send("load_vocab.php");
window['console'].log("Trying to read response");
var rawVocab = response['rawVocab'];
var optionVocab = response['optionVocab'];

上面代码的输出将是:

Sending request
Trying to read response
Error
Now the response returned, setting response variable

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章