Mark
Mark

Reputation: 3

Phonegap FileTransfer and JQuery Mobile JSON Response

i need some help fom the experts.

I want upload a picture to my php script and it works. Now, i want get back a JSON Response after upload to my div #showdata, but it don't work :(

Start Transfer Upload

     var options = new FileUploadOptions();
                options.fileKey="file";
                options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
                options.mimeType="image/jpeg";

                var params = new Object();
                params.message = pmsg;
                options.params = params;
                options.chunkedMode = false;
        var ft = new FileTransfer();
        ft.upload(imageURI, server, function(r) {
        document.getElementById('camera').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";  
        $.mobile.hidePageLoadingMsg();

 // here get the Json response

    $('#showdata').html("<p>item1="+r.item1+" item2="+r.item2+" item3="+r.item3+"</p>");}, function(error) {
    document.getElementById('camera').innerHTML = "Upload failed: Code = "+error.code;              
    }, options); }

Can you help me, how i display the JSON results. Thank you!

Upvotes: 0

Views: 3819

Answers (2)

user5851649
user5851649

Reputation:

the response from the server PHP page:

return json_encode(array('item1'=>'value1', 'item2'=>'value2'));

parse FileUploadResult in javascript:

try {
    var obj = JSON.parse(FileUploadResult.response); // Produces a SyntaxError
} catch (error) {
    console.log(error.message); // Handle the error - Unexpected token
}

try using eval:

eval('var obj=' + FileUploadResult.response);

Upvotes: 0

Simon MacDonald
Simon MacDonald

Reputation: 23273

So in your success function you get an FileUploadResult which you have referred to as "f". Well f has a property called "response" which gives you the response from the PHP page. You'll have to look at the response and use JSON.parse() to turn it into and Object.

Upvotes: 3

Related Questions