我收到以下错误:
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(数据)向我显示有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(data)是:
{"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"]}
这是一个字符串,但我编辑它是为了更容易阅读,它确实返回了所要求的其余信息,但它是一个长字符串,我没有为了简洁而添加它。
答案 0 :(得分:1)
检查 console.log 以查找data.isbn
,确保数据存在,并在声明变量但未初始化时给出javascript 'TypeError: data.isbn is undefined'
。所以它应该是if(typeof data.isbn == 'undefined')
,我猜data.isbn
在这种情况下是null
。
答案 1 :(得分:0)
我不确定你能不能use data.isbn
。它可能必须是data["isbn"]
,如果在您的后端脚本中使用json_encode(array(...))
假设它是PHP。
答案 2 :(得分:0)
您是否在data
内捕获了多个对象?
我假设你的回答如下:
data
isbn
0
sku
price
1
sku
price
...
是这样的吗?
另外,尝试将data
对象作为数组访问,看看它是否会改变结果:
使用:
data['isbn']
而不是
data.isbn
答案 3 :(得分:0)
我建议更改这行代码:
if(data.isbn == null ){
到
if(data.isbn === null ){ // triple '=' sign
更多信息:Comparison Operators
答案 4 :(得分:0)
好的,所以我觉得自己很有竞争力!我有datatype: "json"
。它必须是dataType: "json"
。 dataType
中的“T”需要大写。现在一切都很好。感谢所有试图帮助我的人。我很感激!