0

On successful file transfer to server in jquery (Cordova file transfer), I get object as response.

Now object has array in it, something like:

 Array
    (
        [file] => Array
        (
            [name] => [email protected]
            [type] => image/jpeg
            [tmp_name] => /tmp/php4TR9pw
            [error] => 0
            [size] => 348392
        )
    )    

How can I get value of name?

4
  • array[1][1] this way with js. Commented Jul 24, 2015 at 13:20
  • @Jai - I did some edit. Thanks Commented Jul 24, 2015 at 13:22
  • array['file']['name'] would give you that. if you could post js array, that would be nice to answer. Commented Jul 24, 2015 at 13:24
  • 1
    that looks like a print of a php array ... is that what is actually returned? Need to json_encode if it is Commented Jul 24, 2015 at 13:26

2 Answers 2

1

Code shown looks like the print of a php array. That is not a valid response to be read in an ajax request. Use json_encode() to echo json representation of the array

echo json_encode($data);

I have no familiarity with cordova file transfer so you may or may not need to parse the response using JSON.parse(results) to convert to javascript object.

If this transfer is doing this using $.ajax setting the dataType to 'json' will do the parsing internally

Sign up to request clarification or add additional context in comments.

5 Comments

I did echo json_encode(array('status' => 'success','message'=> 'success','shorturl'=>$short)); in php and in jquery If I did json_stringyfy(r.response) I get result as {"status":"success", "message":"success", "shorturl": "abcde"}
If I want value of shorturl, how can I get it?
For your information charlie, you're not able to use php commands inside cordova projects. And also you not have to encode the data, because an exchange is only possible via ajax and in ajax you can specify your output type. +1
@Sithys did you really think I expected that php was running locally...for an upload? You don't upload locally either ... you upload to server ...which appears to be running php and that php output appears to be print_r($data) which is useless in ajax response
of course not, it's only for your information but at most for the information of people finding this question.
0

Like charlietfl already said, a normal way of handling data between the device and a server is ajax which is provided by jQuery. An Ajax function for retrieving data from a server would look like this:

function getDataFromServer() {
    var term=null;
    $.ajax({
        url:'http://yourUrlGoesHere.tld',
        type:'GET',
        data:term,
        dataType:'json', //defines what you get back from the server

        error:function(jqXHR,text_status,strError){},
        success:function(data) //{
                for (i=0; i < data.items.length; i++){
                                    //This is the part, where you can do what you want with anything inside the array. 
                console.log(data);  //i would simply do this to first see the result inside the console
                }

        }
    })                                                                          
}

So, belonging to your comment, you should save your array inside sth. for example called myArray. Now shorturl would be accessable via myArray.shorturl.

Try something like console.log(myArray.shorturl);

1 Comment

you can't upload using GET

Your Answer

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.