views:

40

answers:

2

I have been thinking about this. I am aware you can use json_encode(); to send an encoded array or multi-dimensional array via the jquery ajax call which will interpret the json array for you.

However I am not aware of any other techniques to send more than just a string using echo, print or return from PHP to the jQuery script?

Can someone please enlighten me on this?

Thanks,

Stefan

+3  A: 

At the end of the day, you will only be sending a string back to an AJAX request. How you interpret that string in the JavaScript is the part that will affect what you are doing.

For instance, you can return XML, JSON, plain text, HTML. You can return a CSV file, and parse it in JS. You could even return Markdown if you wanted, then process it with Showdown.

However, you are only processing strings, not other forms of data. Whatever you can accept and process with JS you can send to an AJAX request as a string.

Doug Neiner
That cleared up my understanding of Ajax - thanks very much :)
Stefan
@Stefan, sure thing! Glad I could help!
Doug Neiner
A: 

True you can't really transmit other data types since anything you send over http is always going to be a string.

However, in theory you can send binary data in that string. You could send a raw image, and assemble it on the other side, or the contents of an mp3. Likewise, if you really wanted, you could send binary code and interpret it.

That might sound silly, but there are practical uses for such methods :).

Jeff Davis