php - TypeError : data. isbn 未定义

标签 php javascript mysql ajax json

我收到以下错误:

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。谁能看出错误出在哪里?我在另一个程序中使用相同的代码并且工作正常。

编辑:这是从中获取信息的 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"]}

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

最佳答案

检查console.log中的data.isbn,您将确定数据在那里并且javascript'TypeError:data.isbn未定义' 在声明变量但未初始化时给出。所以它应该是 if(typeof data.isbn == 'undefined') 并且我猜在这种情况下 data.isbnnull

关于php - TypeError : data. isbn 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11691349/

相关文章:

php - 以 Web 方式运行后台 PHP 脚本时出现超时问题

php - 禁用 cookie 时 session 是否有效?

mySQL 查询多个分组依据

javascript - 使用选择的 jquery 插件获取每个选择的值

JavaScript SSE 不适用于 Firefox

php - 将两个不同数据库中两个不同表的行匹配在一起

php - Laravel 获取数据 where carbon 上个月

php - 使用 xpath 将无序列表 html 标记转换为多维数组

php - Swift 3 上传图像到 PHP 失败

javascript - 更新未在 aframe 组件中触发