0

I was receiving the following error:

TypeError: data.isbn is undefined

I have since added in the data.isbn ==null statement in the following code:

$.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

This now returns No Records Found, however, my console.log(data) is showing me that there are 19 isbn's. Can anyone see where the error lies? I am using identical code in another program and it works fine.

EDIT: Here is the PHP file that it gets the info from:

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) is:

{"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"]}

This is one string, but i edited it for easier reading and it does return the rest of the information asked for, but it is a long string and I did not add it for brevity.

4

5 回答 5

1

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

于 2012-07-27T16:02:02.340 回答
0

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

于 2012-07-27T15:53:18.557 回答
0

我建议更改这行代码:

if(data.isbn == null ){

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

更多信息:比较运算符

于 2012-07-30T17:26:13.407 回答
0

您是否在其中捕获多个对象data

我假设您的回复如下所示:

data
     isbn
         0
           sku
           price
         1 
           sku
           price
         ...

是这样吗?

另外,尝试将您的data对象作为数组访问,看看它是否会改变结果:

利用:

data['isbn']

代替

data.isbn
于 2012-07-27T15:55:20.620 回答
0

好的,所以我觉得自己像个竞争白痴!我有datatype: "json"。它需要是dataType: "json"。中的“T”dataType需要大写。现在一切正常。感谢所有试图帮助我的人。我很感激!

于 2012-07-30T19:39:18.517 回答