AJAX-请求响应后img标记中的怪异字符 [英] AJAX - Weird characters in img tag after request response

查看:81
本文介绍了AJAX-请求响应后img标记中的怪异字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决我提出的AJAX请求的问题(顺便说一句,我是AJAX的新手).我有一个API设置,我想使用一个Authorization标头检索png图像,该标头使用我提供的令牌(存储在本地存储中).因此,例如,如果我要使用Auth标头访问图像,则可以执行此操作...

I'm have trouble solving an issue with an AJAX request I'm making (I'm new to AJAX by the way). I have an API set-up and I'd like to retrieve a png image using an Authorization header which uses a token that I supply (which is stored in local storage). So for example, if i were to access the image with the Auth header then I would do this...

$.ajaxSetup({headers: {"Authorization" : localStorage.token}});

我能够检索图像.我可以在Chrome的网络"标签中看到它,但是当我使用下面的代码将其附加到div时...

I'm able to retrieve the image. I can see it in the "Network" tab in Chrome, but when I append it to my div using this below...

$.ajax({
    //Use commas for several parameters
    type: 'GET',
    url: *url of image*,
    contentType: 'image/png',
    success: function (data) {
    binary = data;
    $("#image").attr("src", 'data:image/png;base64,'+ data);
}

...它以这种奇怪的字符格式显示(如下所示):

...it comes out in this weird character format (as seen below):

div id ="image" src ="data:image/png; base64,PNG

div id="image" src="data:image/png;base64, PNG

IHDRww ^ÀþIDATxÚìÝ|ÔWº?þ½¿ÿ½Ý.ÅâÉd2îdâ®BBÁÝÝ )"ÞBÅÝ¥TÐzi)ÔRÙn»rï]»+ w·{þçùÌ<Ãd]ùýV¾çõz¿ 等等

IHDRww^ÀþIDATxÚìÝ|ÔWº?þ½¿ÿ½Ý.ÅâÉd2îdâ®BB ÁÝÝ )îÞbÅÝ¥TÐzi)Ô ÞRÙn»rï]»+w·{þçùÌ<Ãd]ùýV¾çõz¿H&I°Ï÷ç<çï}OÊø;æO1ªzhÔÀdÆþKȤ!......." etc

我希望它以图像或b64字符串的形式返回,可以将其简单地放入src参数中.我到处都在网上搜索,似乎找不到答案.

I'd like it to come back as an image or a b64 string which I can simply put into the src param. I've searched online everywhere and cannot seem to find an answer.

有什么想法吗?非常感谢您的时间.

Any ideas? I appreciate your time greatly.

这是我收到的回复

推荐答案

似乎您正在获取二进制数据格式的图像文件,而不是希望将其放入img标签src属性的base64格式.

It seems like you are getting image file in binary data format and not the base64 format as you are expecting to put in the img tag src attribute.

如果您有图片网址,则可以直接将其放在img src中,而无需进行ajax调用.

If you do have image url you can put it in the img src directly no need of ajax call.

这篇关于AJAX-请求响应后img标记中的怪异字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆