简体   繁体   中英

Uncaught Type Error in AJAX success object

In the success of one of my AJAX Posts, there's a logical test with the returned "data" object. Upon the page load, Javascript gives an uncaught type error: cannot read property of undefined on that line:

success: function (data) {
    if (data[0].ProductID == 1) { //ERROR HERE ON PAGE LOAD, BUT WORKS IN USE

The code runs correctly during operation, but yet this error persists upon page load. I've tried using .val(), .value, and ['ProductID'] to refer to the data point, and also combined it with "&& data[0].ProductID != 'undefined'" and wrapped the entire section in another if to check for undefined "if (data[0].ProductID != 'undefined') {", but the error still appears. I've tested that the value is never null, and the code works in use, but I'm uncomfortable with just letting it go. Does anyone have an idea of what's causing this? Thanks in advance!

test if data[0] is not undefined:

if (typeof data[0] != 'undefined' && data[0].ProductID == 1) {

}

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