TheExit
TheExit

Reputation: 5320

jQuery Uploadify - How to use onComplete?

I'm using uploadify: http://www.uploadify.com/

And have an onComplete:

onComplete: function(response) {
alert(response);
},

My server is sendin back album_id... How do I access that in the response?

Thanks

UPDATING

        onComplete: function(response) {
            jsonObject = jQuery.parseJSON(response.response);
            alert (jsonObject);
            alert(jsonObject.album_id);
        },

both of the alerts don't run?

UPDATE 2 RAils code that is sending back the JSON? Maybe this is the issue?

render :json => { :result => 'success', :album_id => 31313113 }

Upvotes: 2

Views: 23652

Answers (4)

Einlanzer
Einlanzer

Reputation: 534

//You have to parse it first as a whole object

jsonObject = jQuery.parseJSON(response);

//Then access the object property or method after

alert(jsonObject.album_id);

Upvotes: 0

hardba11
hardba11

Reputation: 1516

The answers above are correct in pointing you towards the onComplete method. The only thing I have to add, is to ask you to post your entire uploadify call. The onComplete needs to be built in your call. It should look like something like this.

$('#sampleFile').uploadify({
        'uploader': 'include/uploadify/uploadify.swf',
        'script': 'add_list.php',
        'scriptData': {'mode': 'upload'},
        'fileDataName': 'newUpload',
        'folder': '/work/temp/uploads',
        'cancelImg': 'images/cancel.png',
        'queueID': 'uploadQueue',
        'onComplete': function (event, queueID, fileObj, response, data) {
        // A function that triggers when a file upload has completed. The default 
        // function removes the file queue item from the upload queue. The 
        // default function will not trigger if the value of your custom 
        // function returns false.
        // Parameters 
        //    event: The event object.
        //    queueID: The unique identifier of the file that was completed.
        //    fileObj: An object containing details about the file that was selected.
        //    response: The data sent back from the server.
        //    data: Details about the file queue.
    }
});

Upvotes: 0

Lorenzo
Lorenzo

Reputation: 29427

the onComplete is sending four arguments. So you function should be like this one:

onComplete: function(event, queueID, fileObj, response, data) {
    alert(response.responseText);
    return false;
},

The return false is needed to avoid to trigger the default function.

Upvotes: 4

Jack Marchetti
Jack Marchetti

Reputation: 15754

I believe the response sent back is:

 function UploadComplete(event, queueID, fileObj, response, data) { }

Response would obviously be whatever you're returning. In my case it was a flickrphotoID because my uploadify script was uploading the file to Flickr then waiting for the ID.

If your response is a json object, then you'll need to parse it out.

Upvotes: 1

Related Questions