POST请求的图像响应中的奇怪字符 [英] Weird characters in image response of POST request

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

问题描述

我必须向API发出POST请求,并且此API返回我要在画布或img元素上显示的图像.问题是奇怪的字符正在响应中,但是在网络"选项卡中,图像正确显示(下面的屏幕截图)

I've to make POST request to an API and this API returns an image which I want to show on canvas or img element. The problem is weird characters are coming in response but in network tab, image is shown properly(screenshots below)

我已经尝试了 AJAX-奇怪的字符中提到的所有内容在请求响应后的img标签中

我尝试转换为base64,使用new Image()加载,尝试转换为blob,使用FileReader()加载,但是加载了空白图像.

I've tried to convert to base64, load using new Image(), tried to convert to blob, load using FileReader(), but blank image is loaded.

如果我在new Image()FileReader()中使用一些静态图像URL,而不是POST请求,那么它可以正常工作.如何渲染来自POST请求的响应图像?感谢您的帮助.

Instead of POST request, if I use some static image URL with new Image() or FileReader(), it works without issues. How do I render an image that comes as a response from POST request? Any help is appreciated.

推荐答案

fotinakis 在发出POST请求时,必须将responseType设置为arraybuffer,然后可以将其转换为base64.问题是从二进制文件获取base64.除非另行设置,否则默认情况下,ajax的成功回调将返回二进制.

While making POST request itself, the responseType has to be set to arraybuffer which can later be converted to base64. Getting base64 from binary was the issue. The success callback of ajax returns binary by default unless set otherwise.

其他responseType可以在 https:中找到://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/responseType .

尽管如此,我还是一无所知.静态图像URL(GET)还返回new Image()没有问题的二进制数据,但是如果我从POST请求成功回调中传递相同的二进制数据,它将不起作用.

I still don't understand one thing though. Static image URL(GET) also returns binary data which gives no issues with new Image() but if I pass the same binary data from POST request success callback, it doesn't work.

这篇关于POST请求的图像响应中的奇怪字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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