简体   繁体   English

第一次单击无法初始化我的变量,但第二次单击可以

[英]Initializing my variable doesn't works by first click, but by second click it works

if I enter an article number, for example 100100 and then click on the button, my array parent is empty, although it actually has to be overwritten by the method called.如果我输入商品编号,例如 100100,然后单击按钮,我的数组父级为空,尽管它实际上必须被调用的方法覆盖。 however, when I click the button a second time, the array is no longer empty.但是,当我第二次单击该按钮时,数组不再为空。 Why is that?这是为什么? I want my array to be overwritten after the first click.我希望我的阵列在第一次点击后被覆盖。

my javascript:我的 javascript:


$(document).ready(function(){
    getAlleArtikelstücklisten();
})

function getAlleArtikelstücklisten(){
    $.ajax({
        url: "http://localhost:8080/orderLists/open",
        type: 'GET',
        contentType: "application/json",
        success: handleData
      });
}
function handleData(data){
    alleArtikelstücklisten=data;
    console.log(alleArtikelstücklisten);
}

Its because the console.log gets called before the getParent() function finishes.这是因为console.loggetParent() function 完成之前被调用。 It is returning the value from the first click/call when you click it the second time.当您第二次单击它时,它将返回第一次单击/调用的值。

You can pass async: false, to fix it.您可以传递 async: false 来修复它。

$.ajax({
        url: "http://localhost:8080/m_stücklistenelement/selectByArtikelnummerStückliste?artikelnummerStückliste="+artikelnmr,
        type: 'GET',
        contentType: "application/json",
        success: handleDataParent,
        async: false
      });

could you have forgotten function parentheses in the 'success' calls?您是否忘记了“成功”调用中的 function 括号?

success: handleData(data)

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

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