I have made a XMLHttpRequest with the 'responseType' set to 'blob' to a php script, is it possible i return more than one type of response? If script can run successfully, response with 'contentType/application/zip' but if the script failed, response with 'string'(error text). I try to response a string in my php script but, in my javascript, i set the XHR.responseType = 'blob'.
In PHP
if(someThingWrong)
{
echo "Archive not found"; //Repsonse is DOMString
header("HTTP/1.1 404 Not Found");
exit;
}
else
{
header("Content-type: application/zip"); //Response is octe-Stream or zip
header("Content-Disposition: attachment; filename=\"" . basename($newZipFile) . "\"" );
readfile($newZipFile);
}
In Javascript
XHR.open("POST", "anycode.php");
XHR.responseType = "blob";
When i try to access the xhr.ResponseText, it show 'undefined'.
if(XHR.status == 404)
{alert(XHR.ResponseText);}
Any help is greatly appreciated.
You can use a FileReader object to get the response as text.
if(XHR.status == 404){
var fr = new FileReader();
fr.onload = function(e){
alert(e.target.result);
}
fr.readAsText(XHR.response);
}