I am trying to upload a picture using jquery. and the upload should be done with no page refresh.
the code:
var uploader = new qq.FileUploaderBasic({
element: document.getElementById('file-uploader'),
button: $('#account #picture .header')[0],
action: '<?= Route::url('Account Upload Avatar'); ?>',
allowedExtensions: ['png', 'jpg', 'gif'],
onSubmit: function()
{
},
onComplete: function(id, fileName, avatar)
{
$('#loader').hide();
$('#picture img').attr('src', responseJSON + '?' + (new Date).getTime());
}
});
and where i have the picture upload:
<div id="picture">
<div class="header">
Schimba-ti poza
</div>
<img src="<?= $image->avatar_url ?>"></img>
</div>
the problem is:
the upload is done right, but i can see the new picture only if i refresh the page. otherwise, when i upload a new picture, the old one dissapears, and instead appears an image broken icon, and if i inspect the element, i see: <img id="picture" src="undefined?1308840720252"> so i get that undefined.. and i guess it is because that attr.
i am using jquery 1.6.1
any ideas?
thank you!
2 Answers 2
Make sure there is a defined variable at avatar.url <-- which could be the cause of the issue
18 Comments
avatar or avatar.url?console.log(avatar);?responseJSON as it is defined in the spec instead of avatarYour server side could should be returning valid JSON which will be parsed and sent to onComplete as the third function parameter.
If the JSON can't be received you'll receive an empty object.
Do:
console.log(avatar);
inside the callback to check what's actually in that third parameter.
propand what withattrprop()function works... (you can see my question stackoverflow.com/questions/5874652/prop-vs-attr for the debate)