未捕获的TypeError:无法读取未定义的属性“0” [英] Uncaught TypeError: Cannot read property '0' of undefined
本文介绍了未捕获的TypeError:无法读取未定义的属性“0”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直得到这个奇怪的错误而我无法弄清楚
I've been getting this weird error and I just can't figure it out
这是我目前的代码:
function addBackground() {
var penguin_id = getPenguinID();
var file = $('#image_form')[0].files[0];
console.log('Image Getting Uploaded');
var formData = new FormData();
formData.append('file', file);
$.ajax({
url: "custBG.php",
type: "POST",
data: formData,
cache: false,
contentType: false,
processData: false,
mimeType: 'multipart/form-data'
}).done(function(data) {
if (data.status == 0) {
console.log('Image Has Been Uploaded');
addBGItem(70000 + penguin_id);
} else {
console.log(data.message);
}
});
}
这是表格
<form id="image_form">
<div class="left">
<img id="img-uploaded" src="http://dummyimage.com/210x210/dbdbdb/7e7f80.png" alt="your image" />
</div>
<div class="right">
<input type="file" name="imageToUpload" accept=".png,.jpeg,.jpg,.gif" onchange="validate_image()">
<span class="btn btn-large btn-alpha" onclick="addBackground()">Upload Image</span>
</span>
</div>
</form>
我不确定是什么,但任何人都可以帮我解决这个问题。
I'm not sure what's up but can anyone help me out with the problem.
推荐答案
而不是 $('#image_form')[0] .files [0];
尝试使用 $('#image_form')。prop('files')[0];
它适用于我。
Instead of $('#image_form')[0].files[0];
try with $('#image_form').prop('files')[0];
It worked for me.
这篇关于未捕获的TypeError:无法读取未定义的属性“0”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文