Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What data formats can AJAX transfer?

I'm new to AJAX, but as an overview I'd like to know what formats you can upload and download. Is it limited to JSON or XML or can you even send binary types like MP3 or UTF-8 HTML. And finally, do you have full control over the data, byte for byte in something like a byte array, or is only a string sent/received.

like image 390
Robin Rodricks Avatar asked May 17 '09 11:05

Robin Rodricks


People also ask

Can AJAX send JSON?

According to the AJAX model, web applications can send and retrieve data from a server asynchronously without interfering with the display and the behavior of the existing page. Many developers use JSON to pass AJAX updates between the client and the server.

What is a common format used to send AJAX responses?

XML is the preferred format for AJAX responses because XML is the easiest format for JavaScript to work with. JSON (JavaScript Object Notation) is a lightweight data-interchange format.

What is the preferred data format for AJAX programming?

Although X in Ajax stands for XML, JSON is preferred because it is lighter in size and is written in JavaScript. Both JSON and XML are used for packaging information in the Ajax model.


2 Answers

If we are talking about ajax we are talking about javascript? And about XMLHTTPRequest?

The XMLHttpRequest which is only a http request can transfer everything. But there is no byte array in javascript. Only strings, numbers and such. Every thing you get from an ajax call is a piece of text (responseText). That might be parsed into XML (which gives you reponseXML). Special encodings should be more a matter of the http transport.

The binary stuff is not ajax dependent but javascript dependent. There are some weird encodings for strings to deliver byte data inside in javascript (especially for images) but it is not a general solution.

HTML is not a problem and that is the most prominent use case. From this type of request you get an HTML string delivered and that is added to some node in the DOM per innerHTML that parses the HTML.

like image 52
Norbert Hartl Avatar answered Oct 19 '22 10:10

Norbert Hartl


Since data is transported via HTTP you will have to make sure that you use some kind of encoding. One of the most popular is base64 encoding. You can find more information at: http://www.webtoolkit.info/javascript-base64.html

The methodology is to base64-encode the data you would like to send and then base64-decode the data at the server(or the client) and use the original data as you intended.

like image 20
Daniel Wedlund Avatar answered Oct 19 '22 10:10

Daniel Wedlund