我有php文件,它返回json对象。如果错误我会像这样设置它们,
if(move_uploaded_file($_FILES['file']['tmp_name'][$position], $uploaddir.$name)){
$uploaded[] = array(
'name' => $name,
'file' => 'assets/uploads/'.$name
);
}
else
{
$error[] = array('error' => $_FILES['file']['tmp_name'][$position].' file uploading failed');
}
如果设置了错误,我需要显示它们。否则我想显示数据。到目前为止,我试过这个。但它不起作用
var displayUploads = function(data){
var uploads = document.getElementById('uploads'),
anchor,
x;
console.log(data);
if(data.file === undefined)
{
for(x=0;x<data.length;x=x+1)
{
console.log(data[x].error;
}
}
else
{
for(x=0;x<data.length;x=x+1)
{
anchor = document.createElement('a');
anchor.href = '<?php echo base_url(); ?>'+ data[x].file;
anchor.innerText = data[x].name;
anchor.target = '_blank';
uploads.appendChild(anchor);
}
}
}
答案 0 :(得分:1)
尝试替换此行:
if(data.file === undefined)
用这个:
if(typeof(data.file) === 'undefined')