简体   繁体   中英

get parameter from response API

I got a problem when retrieving the parameters of the response. Anyone can help? :(

this error on console :

katalog_laporan:1837 Uncaught TypeError: Cannot read property 'MENU' of undefined at Object. (katalog_laporan:1837) at Function.each (jquery-3.2.1.slim.js:359) at Object.success (katalog_laporan:1830) at j (jquery-3.2.1.slim.js:3152) at Object.fireWith [as resolveWith] (jquery-3.2.1.slim.js:3212) at x (jquery-3.2.1.slim.js:8159) at XMLHttpRequest. (jquery-3.2.1.slim.js:8159)

this file js me :

                for(x = 0; x <= panjangindex; x++){
                    var fullmenu = response.detail[x].MENU;
                    var itemmenu = fullmenu.split('>');
                    var jumlahitem = itemmenu.length;
                    el.append('<li>');
                       for(i = 0; i < jumlahitem; i++){
                           var item = itemmenu[i]; 
                           el.append('<span  style="font-size:12px" class="label label-info arrowed-right arrowed-in">'+itemmenu[i]+'</span>');  
                       }
                    el.append('</li><br><br>');   
                }

To avoid showing this error you should check availability of you object or array so it will be like

for(x = 0; x <= panjangindex; x++){
var fullmenu = response.detail[x].MENU ?     response.detail[x].MENU : null;

if(fullmenu) {
    var itemmenu = fullmenu.split('>');
    var jumlahitem = itemmenu.length;
    el.append('<li>');
    for(i = 0; i < jumlahitem; i++){
        var item = itemmenu[i]; 
        el.append('<span  style="font-size:12px" class="label label-info arrowed-right arrowed-in">'+itemmenu[i]+'</span>');  
    }
el.append('</li><br><br>');
}
}

And you have to check your api call that its already containing this element by console.log(response.details[x]);

Please try like that --

response.detail[x]['MENU']

instead of --

response.detail[x].MENU

If still not works then please share response variable format via below link.

https://gist.github.com

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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