Php 为什么我得到的typeError值为null,但在我的firebug中,我可以看到有值而不是null?

Php 为什么我得到的typeError值为null,但在我的firebug中,我可以看到有值而不是null?,php,json,jquery,Php,Json,Jquery,为什么我得到的typeError值为null,但在我的firebug中,我可以确定存在值而不是null,有什么问题 以下是我的ajax代码: $.ajax({ url: 'get-products.php', type: 'post', datatype: 'json', data: { category: $('.category').val().trim(), keyword: $('.keyword').val().trim() }, suc

为什么我得到的typeError值为null,但在我的firebug中,我可以确定存在值而不是null,有什么问题

以下是我的ajax代码:

    $.ajax({
    url: 'get-products.php',
    type: 'post',
    datatype: 'json',
    data: { category: $('.category').val().trim(), keyword: $('.keyword').val().trim() },
    success: function(data){
        var toAppend = '';
        toAppend += '<thead><th>Product Name</th><th>Image</th><th>Price</th><th>Weight</th><th>ASIN</th><th>Category</th></thead>';
        if(typeof data === "object"){
            for(var i=0;i<data.length;i++){
                toAppend += '<tr><td>'+
                data[i]['product_name'][0]+'</td><td><img src="'+
                data[i]['image'][0]+'" alt=""></td><td>'+
                data[i]['price'][0]+'</td><td>'+                            
                data[i]['weight']+'</td><td>'+                                      
                data[i]['asin'][0]+'</td><td>'+                                         
                data[i]['category'][0]+'</td></tr>';
            }
            $('.data-results').append(toAppend);
        }
    }
});
以下是我的firebug的结果:

这是我的示例输出,我是否仍然可以显示结果,即使像在图像中一样仍然有空结果?如果图像为空,则没有显示图像


在firebug图像中,它似乎表示图像在第7个索引处为空。 因此,如果您执行data.image[index],则访问的内存位置无效。image属性必须指向某个对象

             for(var i=0;i<data.length;i++){
//You can save default image in a global variable, hidden div... it is up to you. 
                var img ;
                if(data[i]['image'] === null){
                  img = defaultImage ;
                }
                else
                { 
                    img = data[i]['image'][0];  
                }
                toAppend += '<tr><td>'+
                data[i]['product_name'][0]+'</td><td><img src="'+
                img +'" alt=""></td><td>'+ //use img here
                data[i]['price'][0]+'</td><td>'+                            
                data[i]['weight']+'</td><td>'+                                      
                data[i]['asin'][0]+'</td><td>'+                                         
                data[i]['category'][0]+'</td></tr>';
            }

for(var i=0;请参见firebug中的第7个元素,此处的图像为空。请注意,您遗漏了一个
+
数据[i]['weight']+''
@未定义,我的真实代码中有+,我的错误,所以我怎么还能显示结果?我会调试你的服务器端代码,看看为什么元素缺少图像。你给我们的代码中没有太多信息。实际上,我正在使用AWS API从AMazon获取数据,我无法防止数据为空,我仍然可以显示吗播放结果?添加一条if语句,查看它是否指向null。如果它指向null。如果它指向null。请在将html代码串在一起之前执行此操作。我将编辑我的答案。
             for(var i=0;i<data.length;i++){
//You can save default image in a global variable, hidden div... it is up to you. 
                var img ;
                if(data[i]['image'] === null){
                  img = defaultImage ;
                }
                else
                { 
                    img = data[i]['image'][0];  
                }
                toAppend += '<tr><td>'+
                data[i]['product_name'][0]+'</td><td><img src="'+
                img +'" alt=""></td><td>'+ //use img here
                data[i]['price'][0]+'</td><td>'+                            
                data[i]['weight']+'</td><td>'+                                      
                data[i]['asin'][0]+'</td><td>'+                                         
                data[i]['category'][0]+'</td></tr>';
            }