类型错误:data.isbn未定义


TypeError: data.isbn is undefined

我收到以下错误:

TypeError: data.isbn is undefined

此后,我在data.isbn ==null语句中添加了以下代码:

$.ajax({
    type: "POST",
    url: "getInventory.php",
    datatype: "json",
    data: ({skuStart: $("#startSkuRange").val(), skuEnd: $("#endSkuRange").val(),
        processDate: $("#processDate").val(), source: $("#source").val()}),
    success: function(data) {
    console.log(data);
    if (data.isbn == null) {
            $("#inventoryUpdate").append('<tr><td>No Records Found</td></tr>');
        } else {
            for(var x=0; x<data.isbn.length; x++) {
                $("#inventoryUpdate").append('<tr><td id="tableSKU">'+data.sku[x]+'</td><td id="tableISBN">'+data.isbn[x]+
                    '</td><td><input type="text" id="tableQuantity" value="'+data.quantity[x]+
                    '"/></td><td><input type="text" id="tableDefect" value="'+data.defect[x]+
                    '"/></td><td><input type="text" id="tableSource" value="'+data.source[x]+
                    '"/></td><td><input type="text" id="tableFeature" value="'+data.feature[x]+
                    '"/></td><td><input type="text  id="tableLocation" value="'+data.location[x]+
                    '"/></td><td><input type="text" id="tableProcessDate" value="'+date.processDate[x]+
                    '"/></td><td><input type="text" id="tableBookType" value="'+data.booktype[x]+
                    '"/></td><td><input type="text" id="tableCreatedBy" value="'+data.created[x]+
                    '"/></td><td><input type="text" id="tableModifiedBy" value="'+data.modified[x]+
                    '"/></td></tr>');
            }
            $("#inventoryUpdate").trigger("update");
        }
    }
});// end of ajax call

现在返回No Records Found,但是,我的console.log(data)显示有19个isbn。有人能看出错误在哪里吗?我在另一个程序中使用相同的代码,它运行良好。

EDIT:这是它从中获取信息的PHP文件:

if(!empty($start) && !empty($end)){
    $result = $conn->query("Select * from inventory where sku >= $start and sku <= $end");
} elseif (isset($start) && isset($end) && isset($source)){
    $result = $conn->query("Select * from inventory where sku >= '$start' and sku <= '$end' and source_id = '$source'");
} elseif (isset($processDate)) {
    $result = $conn->query("Select * from inventory where date_process = '$processDate'");
} else {
    $result = $conn->query("Select * from inventory where sku >= '$start' and sku <= '$end' or source_id = '$source' or date_process = '$processDate'");
}
while($row = $result->fetch_assoc()) {
    $skuArray[$x] = $row['sku'];
    $isbnArray[$x] = $row['isbn13'];
    $qtyArray[$x] = $row['quantity'];
    $defectArray[$x] = $row['defect_id'];
    $sourceArray[$x] = $row['source_id'];
    $featureArray[$x] = $row['feature_id'];
    $locationArray[$x] = $row['location_id'];
    $processDateArray[$x] = $row['date_process'];
    $bookTypeArray[$x] = $row['book_type_id'];
    $createdByArray[$x] = $row['created_by'];
    $modifiedByArray[$x] = $row['modified_by'];
    $x++;
} // end of while loop  
$results = array('sku' => $skuArray,
                 'isbn' =>$isbnArray,
                 'quantity' => $qtyArray,
                 'feature' => $featureArray,
                 'processDate' => $processDateArray,
                 'source' => $sourceArray,
                 'location' => $locationArray,
                 'created' => $createdByArray,
                 'modified' => $modifiedByArray,
                 'booktype' => $bookTypeArray,
                 'defect' => $defectArray,
                 );
$conn->close();
echo json_encode($results);

console.log(数据)是:

{"sku":["10123400","10123401","10123402","10123403","10123404","10123405","10123406","10123407","10123408","10123409","10123410","10123411","10123412","10123413","10123414","10123415","10123416","10123417","10123418"],
 "isbn":["9781416025405","9780072993288","9780534380311","9780495095538","9780781778107","9780205741786","9780673985255","9780618331505","9780321106766","9780495506218","9780321557537","9780534629915","9780312664817","9780198610298","9780323046343","9780323023108","9781439036402","9780132497992","9780538497817"]}

这是一个字符串,但为了便于阅读,我对它进行了编辑,它确实返回了所需的其余信息,但它是一个长字符串,为了简洁起见,我没有添加它。

检查data.isbnconsole.log,这样您就可以确保在声明但未初始化变量时数据存在,并且会给出javascript 'TypeError: data.isbn is undefined'。所以它应该是if(typeof data.isbn == 'undefined'),我猜在这种情况下data.isbn就是null

是否在data中捕获多个对象?

我假设你的回复看起来像:

data
     isbn
         0
           sku
           price
         1 
           sku
           price
         ...

是这样吗?

此外,尝试将data对象作为数组访问,看看它是否会更改结果:

用途:

data['isbn']

代替

data.isbn

我建议更改这行代码:

if(data.isbn == null ){

if(data.isbn === null ){ // triple '=' sign

更多信息:比较运算符

我不确定你能不能use data.isbn。如果在后端脚本中使用json_encode(array(...)),假设它是PHP,那么它可能必须是data["isbn"]

好吧,所以我觉得自己像一个竞争对手MORON!我得了datatype: "json"。它需要是dataType: "json"dataType中的"T"需要大写。现在一切都很好。感谢所有帮助我的人。我很感激!