0

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!

asked Jun 23, 2011 at 13:56
11
  • what does a console.log(avatar) give you? Commented Jun 23, 2011 at 13:59
  • I think you should use prop instead of attr Commented Jun 23, 2011 at 13:59
  • Can you console.log avatar.url in onComplete to see its value? Commented Jun 23, 2011 at 13:59
  • 1
    @Karolis -- read the chart on this site blog.jquery.com/2011/05/12/jquery-1-6-1-released to see what should be used with prop and what with attr Commented Jun 23, 2011 at 14:03
  • 1
    @Karolis, that is how the new prop() function works... (you can see my question stackoverflow.com/questions/5874652/prop-vs-attr for the debate) Commented Jun 23, 2011 at 14:15

2 Answers 2

3

Make sure there is a defined variable at avatar.url <-- which could be the cause of the issue

answered Jun 23, 2011 at 13:59
Sign up to request clarification or add additional context in comments.

18 Comments

yes, i receive in the console an error: ReferenceError: avatar is not defined but i cannot really understand it. how can i define the variable at avatar.url? thanks!
@dana -- this is for avatar or avatar.url?
also for avatar.url: console.log(avatar.url) and console.log(avatar)
@dana you tried @Alnitak's suggestion of console.log(avatar);?
@dana try using responseJSON as it is defined in the spec instead of avatar
|
0

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

answered Jun 23, 2011 at 14:01

2 Comments

i have commented above. it is not defined(the avatar)
which suggests that you're not sending valid JSON. Next please show us what your server-side script is outputing.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.