Skip to content
Advertisement

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. 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:

$(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);
}

Advertisement

Answer

Its because the console.log gets called before the getParent() function finishes. 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.

$.ajax({
        url: "http://localhost:8080/m_stücklistenelement/selectByArtikelnummerStückliste?artikelnummerStückliste="+artikelnmr,
        type: 'GET',
        contentType: "application/json",
        success: handleDataParent,
        async: false
      });
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement