SJSSoft SJSSoft - 1 year ago 76
Javascript Question

JSON response at ajax request to JS variables

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

In detail, I have 2 page

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




It is working fine. Then on
, 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.

Answer Source

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);

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

    alert(jsDlpath+" - "+jsPhotoIDPath)

NOTE : Use success/done callback instead of complete.

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download