2

I am trying to upload files with dynamic names and get those dynamic names back.

In detail, I have 2 page form.php and upload.php. When upload button is pressed on form.php then request sent to upload.php, where 2 files (DLpath and PhotoIDPath) are uploaded to server with dynamically names e.g :

DLpath=documents/20161130232311i8hGn0HzJT276415832.png

And

PhotoIDPath=documents/20161130232311SSRqCyIbKKalock.png.

It is working fine. Then on upload.php, I am encoding those file names as JSON array i.e.

$response  = array ('DLpath'=>$Dlpath ,'PhotoIDPath'=>$PhotoIDPath);
echo json_encode($response);

And firebug snapshot is :

enter image description here

I want to get DLpath in var jsDlpath and PhotoIDPath in var jsPhotoIDPath

And my code ( Not working) to get response is :

complete: function(response) 
    {
    var jsDlpath=response.DLpath;
    var jsPhotoIDPath=response.PhotoIDPath;
alert(jsDlpath+" - "+jsPhotoIDPath)
}

And alert show :

undefined - undefine

If you can help me to gwt those values in js variables, I will be very thankful to you.

5
  • have you parsed the response? .. try loging the response and if it's a string.. use JSON.parse to parse it to json object Commented Nov 30, 2016 at 18:20
  • 1
    What are you receiving in response? Can you post here? Commented Nov 30, 2016 at 18:27
  • 1
    I think you should do it in success no complete Commented Nov 30, 2016 at 18:31
  • 1
    console.log(response) shows what? Commented Nov 30, 2016 at 18:32
  • When I tried : var obj=JSON.parse(response); It gives error "SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data" AND $("#returned").html(response.responseText); is giving {"DLpath":"documents\/20161201000303tHD2cxzz0u276415832.png","PhotoIDPath":"documents\/20161201000303ZGeDG1odOEalock.png"} Commented Nov 30, 2016 at 18:34

2 Answers 2

2

Since you're encoding you response in server side you should parse it in the js side, you could use $.parsejson() :

success: function(response) 
{
    var response = $.parseJson(response);
     //if $.parseJson dont work, use JSON.parse

    var jsDlpath=response.DLpath;
    var jsPhotoIDPath=response.PhotoIDPath;

    alert(jsDlpath+" - "+jsPhotoIDPath)
}

NOTE : Use success/done callback instead of complete.

Hope this helps.

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

Comments

0

If running in pure javascript you will find that there are two response attributes: responseText and responseXML. You probably want:

var data = JSON.parse(response.responseText);

A complete example, using curl from https://gist.github.com/bitdivine/7ddd943387a4350336dd (but jquery will do fine as well) to get open issues on Github:

curl('https://api.github.com/repos/gchq/CyberChef/issues?state=open')
.then((res) => JSON.parse(res.responseText))
.then((data) => console.log(data))

Comments

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.