2

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?

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173

5 Answers5

4

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
  • 2,063
  • 1
  • 15
  • 22
  • And how do I get the result back as a plain string, not as an object? – Prof. Falken Jul 14 '10 at 14:02
  • 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. – Prof. Falken Jul 18 '10 at 16:45
2

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
  • 87,747
  • 23
  • 163
  • 198
  • 1
    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 Jul 14 '10 at 13:53
  • To download binary data with XMLHttpRequest in a cross-browser fashion, see this answer. http://stackoverflow.com/questions/1095102/how-do-i-load-binary-image-data-using-javascript-and-xmlhttprequest/5913807#5913807 – Cheeso May 06 '11 at 15:52
2

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
  • 114,488
  • 30
  • 148
  • 167
  • 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. :-) – Prof. Falken Jul 14 '10 at 14:01
  • 2
    Just instead of reading the responseXML property, read the responseText property. It's detailed here: https://developer.mozilla.org/en/xmlhttprequest – thomasrutter Jul 14 '10 at 14:15
1

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
  • 307
  • 2
  • 9
1

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
  • 4,907
  • 4
  • 29
  • 42