views:

68

answers:

5

I understand JSON can be used instead of XMLHttpRequest in Javascript, but can I make requests and get totally arbitrary data back?

Like a custom text or binary format?

Or is the interface limited to jSON and XML?

I hope I get through what I wonder here...

a) How do I create a plain request without XML or JSON?

b) How do I access the result (answer) as a plain string, not an object?

+2  A: 

You can use any text-based format: XML, JSON, CSV, or just text/plain.

I'm not sure what happens if you try to use a binary format.

dan04
As in 7-bit ASCII?
Amigable Clark Kant
I believe it should be able to interpret any character encoding the browser can decode and return it in Javascript's own Unicode encoding.
thomasrutter
+2  A: 

You can get the result back as

  • An DOM document (if XML was received) using the responseXML property, or
  • A string (regardless of format) using the responseText property

Some browsers can also return it as an object, if it was JSON.

The ability to get back anything as a string allows you parse any format yourself.

thomasrutter
How do I tell it what I want is a string? Your answer was actually the best answer so far, but the others about the request were also very interesting. I did not know I should be asking that question too. :-)
Amigable Clark Kant
Just instead of reading the responseXML property, read the responseText property. It's detailed here: https://developer.mozilla.org/en/xmlhttprequest
thomasrutter
+4  A: 

Yes, just set the content-type:

var request = new XMLHttpRequest();
request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); // whatever character set you need.
request.open("GET", "yourtext.txt", true);
request.onreadystatechange = function() {
 if(this.readyState == 2) {
  alert(request.responseText);
 }
}
request.send();
Andir
Thanks! This is really useful.
Amigable Clark Kant
And how do I get the result back as a plain string, not as an object?
Amigable Clark Kant
Edited to show you how to get response text.
Andir
It took reading thomasrutters answer about responseText to realize what you meant first, but thanks! :-) I am not used to Javascript. onreadystatechange was not obvious to me.
Amigable Clark Kant
+1  A: 

You can set the content type explicitly to plain text before send():

var request = new XMLHttpRequest();
request.open("POST", "/test.php");
request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
request.send(message);
ttchong
+1  A: 

In addition to the posts mentioning the setRequestHeader method: it seems like the w3 specifications for the xmlhttprequest API only imposes (suggested) restrictions for the header (i.e. the first argument) but not for the value (i.e. the second argument).

Here's the link to the spec: http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method .

AFAIC, this means any data type is valid as far as the requested transfer is concerned. This does not extend to the returned data however as only text and xml are specified.

I might be wrong though.

FK82