简体   繁体   English

在jQuery中调用函数ajax成功响应只能工作一次但不能两次

[英]Calling function inside jQuery ajax success response works once but not twice

I have a page where I enter the names of cards and the card's details are presented via AJAX. 我有一个页面,我输入卡片的名称,卡片的详细信息通过AJAX呈现。 I've removed a lot of code in this example that isn't relevant to the issue and I've tested this simplified code and it reproduces the same error. 我在这个例子中删除了很多与该问题无关的代码,并且我已经测试了这个简化的代码并且它再现了相同的错误。

The mysterious thing is that it works fine the first run through the testSelectedCardDetails function. 神秘的是它在第一次运行testSelectedCardDetails函数时工作正常。 It calls setNameCardIsFromField fine and does what it is meant to do. 它调用setNameCardIsFromField并完成它的意图。 The second time through it dies with the console showing: 第二次通过它死于控制台显示:

Uncaught TypeError: setNameCardIsFromField is not a function

What's up with that? 那是怎么回事? I honestly don't know enough about how jQuery works to be able to nail the problem so I turn to the stackoverflow world begging for salvation from this madness. 老实说,我不知道jQuery如何能够解决这个问题,所以我转向stackoverflow世界,乞求从这种疯狂中获得救赎。 ;) ;)

    function testSelectedCardDetails(cardName) {

        var cardTestInfoArray = [];

        $.ajax({
            url: 'includes/getCardAndSetDataViaAJAX.php',           
            type: 'POST',
            cache: false,
            async: false, // if set to true then the data doesn't get here fast enough to run commands in 'success'
            dataType: 'json',
            data: { callThis: "testSelectedCardDetails", thisCardName: cardName},
            error: function(data){
                console.log("AJAX failed to get card info.");
            },
            success: function(data) {                   

                $.map(data, function (value) { cardTestInfoArray = value; });
                cardPresentInfo['printings'] = cardTestInfoArray['printings'];

                //automatically set setNameCardIsFrom field
                setNameCardIsFromField(cardPresentInfo['printings']);
            }
        });
    }

Here's the function being called: 这是被调用的函数:

    function setNameCardIsFromField(setCode) {

        $.ajax({
            url: 'includes/getCardAndSetDataViaAJAX.php',           
            type: 'POST',
            cache: false,
            async: false,   // if set to true then the data doesn't get here fast enough to run commands in 'success'
            dataType: 'text',
            data: { callThis: "setNameCardIsFromField", thisSetCode: setCode},
            error: function(data){
                console.log("AJAX failed to get card info.");
            },
            success: function(setName) {
                //console.log(setName);
                setNameCardIsFromField = document.querySelector('#setNameCardIsFrom');
                setNameCardIsFromField.value = setName; 
            }
        });
    }

You overwrite the function: 你覆盖了这个功能:

 setNameCardIsFromField = document.querySelector('#setNameCardIsFrom');

Assuming it's global (which is generally bad). 假设它是全局的(通常很糟糕)。

Unrelated, but meh on using async: false , better to do it right than to hack around it, eg, callback, promise, whatever. 不相关,但是使用async: false ,更好地做正确而不是破解它,例如回调,承诺,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM