I am developing mobile applications, and I was thinking, if I need to download a picture from a server, I cannot actually run the PHP natively (I am using a PhoneGap type setup), so how could I download a picture from a database, run it though JavaScript and then display it to the user?
I would imagine something like:
- Ajax request,
- Return HTML string of binary data
- Do some stuff to that?
Or
- Ajax request,
- Return a HTML string of reference to the picture, for example:
picture1.jpg.
In JavaScript, write something likedocument.write <img src="http://blahh/img/"+imagePath
I’m not sure what the best way to do this is.
To my mind, the simplest way to dynamically load external pictures is to get a JSON object from a PHP script containing the picture URL (like
http://www.example.com/pictures/getPicture/YOUR_PICTURE_ID).Server side
Client side
If you don’t use jQuery, you have to use XMLHttpRequest to get the JSON encoded response and to parse it (you can see the MDN documentation at https://developer.mozilla.org/en-US/docs/JSON).