Binary response from webservice, convert to text?

I have access to a webservice that returns binary data. I have set up an ajax request that returns text like the following,

(q/¿M¿ ¿¿¿&PB¿¿¿¿p/¿M A¿¿&PB¿¿¿¿¿=¿MH¿¿&PB¿¿¿¿¿K¿M 2¿¿¿PB¿¿¿¿p/¿M J¿¿¿PB¿¿¿¿¿=¿M G¿¿¿PB¿¿¿¿¿K¿M B¿SYQB¿I¿¿p/¿M T¿

How do I use this data? Is the browser already interpreting it? I can request the data as json but their is so much of it that the request takes >60seconds so we are trying to transfer the data in binary and interpret it on the client. This transfer is an order of magnitude faster.
function loadXMLDoc(){
	var url = "http://10.10.10.10/pa/pa/pd";
	var params = "times=1303916400%2C0%2C0&timerange=0..2&areas=53%2C-8%2C52%2C-8%2C54%2C-8&ndp=1%2C2%2C6%2C7%2C14%2C17%2C18%2C19&server=10.50.50.10&command=grid&obs=";
	
	var xmlhttp = new XMLHttpRequest();

	xmlhttp.onreadystatechange = function(){
	
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
		}
	}


	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);

}

Open in new window

OwenMoriartyAsked:
Who is Participating?
 
GkConnect With a Mentor Commented:
function load_binary_resource(url) {  
  var req = new XMLHttpRequest();  
  req.open('GET', url, false);  
  //XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]  
  req.overrideMimeType('text/plain; charset=x-user-defined');  
  req.send(null);  
  if (req.status != 200) return '';  
  return req.responseText;  
}  
The magic happens in line 5, which overrides the MIME type, forcing Firefox to treat it as plain text, using a user-defined character set.  This tells Firefox not to parse it, and to let the bytes pass through unprocessed.
Refer : http://www1.icyte.com/system/snapshots/fs1/8/5/c/d/85cdb5f77f163918992f0efeaa5b6f3f5008deed/index.html#Receiving_binary_data_using_JavaScript_typed_arrays
0
 
GkCommented:
var xhr = new XMLHttpRequest();
02
xhr.open("GET", "binary_file", false);
03
xhr.send(null);
04
 
05
buffer = xhr.mozResponseArrayBuffer;
06
if (buffer) {
07
  var byteArray = new Uint8Array(buffer);
08
  for (var i=0; i<byteArray.byteLength; i++) {
09
    // do something with each byte in the array
10
  }
11
}
https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Receiving_binary_data



refer :
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.