0

I have the following ajax function:

reader.onload = function(event){
    var fd = new FormData();
    var Name = encodeURIComponent('audio_recording_' + new Date().getMinutes() + '.wav');
    console.log("name = " + Name);
    fd.append('fname', Name);
    fd.append('data', event.target.result);
    $.ajax({
        type: 'POST',
        url: 'upload.php',
        data: fd,
        processData: false,
        contentType: false,
        success: function(data){
            //console.log(data);
             $.ajax({
                    type: 'POST',
                    url: 'readFile.php',
                    data: {"fileName":fileName},
                    success: function(data){
                        console.log(data);
                    }
                });
        }
    });
};      
  1. first question: I want to retrieve the data from the second success function to use it later in the code.how could that happen?

  2. second question: the data is an audio file.Is there is a special way to get audio data, or we can get it the same way as any data?In my php server side of the second ajax, I'm reading an audio file and want to use its data.I did simple file open and get contents.does that work for audio files?

server-side code:

<?php
$fileName=$_POST["fileName"];

$dh = opendir('upload/');
$contents = file_get_contents('C:/wamp/www/JSSoundRecorder/upload/'.$fileName);
// echo $contents;
echo $fileName;
lama
  • 129
  • 1
  • 8
  • first question to be answered is enough for me if someone can help please – lama Jul 17 '14 at 06:42
  • First off, why are you calling another script on the response? Why not do it all in `upload.php` in order to reduce 1 request? Another one, i don't see where the `fileName` variable comes from, are you having any error? What's the output from console? – Teh SoTo Jul 17 '14 at 06:42
  • no im not having any error and all is fine the fileName is declared but I didn't copy the whole code.I just want to use the data from the second ajax later in the program.as for why I'm using another script in the response this has to do with the program flow.bare with me – lama Jul 17 '14 at 06:44
  • the data I want are fileName and fileBuffer for the second ajax how to retrieve these info? – lama Jul 17 '14 at 06:46
  • i don't see a problem in this code, are you sure `readFile.php` is actually returning the data? Can you post the code of `readFile.php`? – Teh SoTo Jul 17 '14 at 06:48
  • there is no problem.the console.log(data) is showing the data I just want to know how can I use this data later in the program,how can I call a function to use this data – lama Jul 17 '14 at 06:50
  • 1
    You can assign the data to a global variable. http://stackoverflow.com/questions/905298/jquery-storing-ajax-response-into-global-variable – JamesA Jul 17 '14 at 06:53
  • Use a global variable as @JamesA said.. or you can use [localStorage](http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage) too... – Teh SoTo Jul 17 '14 at 06:54
  • localStorage is a very good idea thanks a lot – lama Jul 17 '14 at 06:57

1 Answers1

0

This is a bad practice in general, but what you could do is specify a global variable at the start, and then assign data to that variable inside the success. The issue with this is that you can't be certain that the ajax has completed and your variable has been set, before you need to use it.

var mySuccessVar = null;

...
success: function(data) {
    mySuccessVar = data;
}

... // later in the code:
if (mySuccessVar != null) {
    yourFunction(mySuccessVar);
}