javascript - Getting image from Rest API. JQuery issues -
two related questions.
(this jquery.js file) $.ajax ({ type: "get", url: "url", datatype: 'image/png', async: false, beforesend: function (xhr) { xhr.withcredentials = true; xhr.setrequestheader("authorization", "bearer xxxx"); }, success: function (data) { document.getelementbyid("myimage").src = data; // (will in next question) console.log(data); } }); when json data, put data chrome console (developer tools) via console.log. png don't see data. worries me because in request response picture see binary data
�Ս-��tkn�v���6�k�˟>��������7�&=���+���?j�k�����y����l���m�(ψ/ but need make sure in data variable because of .src = data line.
this leads next question.... if image binary data in "data" variable, shouldnt displayed in html?
<html> <head> <script src="//code.jquery.com/jquery-2.0.0.min.js"></script> <script src="jquery.js"></script> </head> <body> <img id="myimage" src="" /> </body> </html> thanks!
why need ajax call @ all? set
document.getelementbyid("myimage").src = url; even if contents of image via ajax call, around browser issues, encoding issues etc., can't display image, becuase "src" attribute of img take url not data stream.
beside that, code seems fine, if go jquery.com , run in console (firefox - latest), datastream:
$.ajax ({ type: "get", url: "http://jquery.com/jquery-wp-content/themes/jquery.com/i/feature-sprites.png", datatype: 'image/png', async: false, success: function (data) { console.log(data); } }); you may want ensure authentication not failing, , since running script on domain different of api should ensure api allows cors (cross origin resouce sharing) means can call different domain.
Comments
Post a Comment