Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a php file that gets an audio blob from a sql database. I need to send that blob to an html page with JavaScript.

Below is my PHP code where I send the blob variable $ubr.

echo json_encode(array('first'=>$ubr));

Below is my JavaScript where I try and receive the blob.

var url = "testThis.php"

var ajax = new XMLHttpRequest();
ajax.open("GET", url, true);
//ajax.send(null);
ajax.onreadystatechange = function () {

    if (ajax.readyState == 4 && (ajax.status == 200)) {
        var Data = JSON.parse(ajax.responseText);               
        var b64Data = Data.first;
    }
}

I think it's because I'm treating it like a string but Blobs are binary data. Except I'm not sure what else to do.

I need to keep the php data as an array. I will need to send a blob and a string value at the same time.

share|improve this question
    
What string value is it that you need to send along? Maybe send the audio as a binary file, with the string information being present in a HTTP header –  Bergi Jun 6 at 4:03

1 Answer 1

For AjaxXMLHttpRequest, Server Response can be

  1. responseText - when you return html or any string as your response.
  2. responseXML - when you return XML as your response.
  3. response - when you return response as an Array-buffer, Blob, Document, JavaScript object (for "json"), or string. This is null if the request is not complete or was not successful.

If you are returning HTML as response ex.PHP code :

    $data['blob']="blob_value";

   return "<div id='blob'>".$data['blob']."</div><div id='string'>".$data['string']."</div>"; // returning HTML as response .

Then you may use responseText

 ajax.onreadystatechange = function () {

             if (ajax.readyState == 4 && (ajax.status == 200)) {

               document.getElementById("target").innerHTML =(ajax.responseText);               

 }

}

In your case for returning array

PHP code can be :

  $data['blob']="blob_value";
  $data['name']="name_value";
  // returning json data ==> {"blob": "blob_value","name": "name_value"}
  return json_encode($data);  

within JavaScript :

   ajax.onreadystatechange = function () {

      if (ajax.readyState == 4 && (ajax.status == 200)) {
         var data= ajax.response;
         var arr= JSON.parse(data);
     alert(arr.blob);                        

       }
     }
share|improve this answer
    
I need to return an array though, not just a string. Also you said "you may use response text" for both, was one supposed to say responseXml? –  SolidCloudinc Jun 6 at 4:32
    
@Silz: You are forgetting xhr.response, which can basically be of any type. Even binary ones. –  Bergi Jun 6 at 4:40
    
@Bergi : Really appreciate your help :) –  Silz Jun 6 at 5:14
    
@SolidCloudinc : Have updated my answer you may check . –  Silz Jun 6 at 5:15
    
@Silz I still get Uncaught Syntax error, unexpected token d on var arr= JSON.parse(data); –  SolidCloudinc Jun 8 at 1:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.