<i id='LxQWI'><tr id='LxQWI'><dt id='LxQWI'><q id='LxQWI'><span id='LxQWI'><b id='LxQWI'><form id='LxQWI'><ins id='LxQWI'></ins><ul id='LxQWI'></ul><sub id='LxQWI'></sub></form><legend id='LxQWI'></legend><bdo id='LxQWI'><pre id='LxQWI'><center id='LxQWI'></center></pre></bdo></b><th id='LxQWI'></th></span></q></dt></tr></i><div id='LxQWI'><tfoot id='LxQWI'></tfoot><dl id='LxQWI'><fieldset id='LxQWI'></fieldset></dl></div>
    1. <tfoot id='LxQWI'></tfoot><legend id='LxQWI'><style id='LxQWI'><dir id='LxQWI'><q id='LxQWI'></q></dir></style></legend>

        <small id='LxQWI'></small><noframes id='LxQWI'>

        • <bdo id='LxQWI'></bdo><ul id='LxQWI'></ul>

        POST 请求的图像响应中的奇怪字符

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

        <small id='L5bHB'></small><noframes id='L5bHB'>

          <tfoot id='L5bHB'></tfoot>
              <tbody id='L5bHB'></tbody>

              <i id='L5bHB'><tr id='L5bHB'><dt id='L5bHB'><q id='L5bHB'><span id='L5bHB'><b id='L5bHB'><form id='L5bHB'><ins id='L5bHB'></ins><ul id='L5bHB'></ul><sub id='L5bHB'></sub></form><legend id='L5bHB'></legend><bdo id='L5bHB'><pre id='L5bHB'><center id='L5bHB'></center></pre></bdo></b><th id='L5bHB'></th></span></q></dt></tr></i><div id='L5bHB'><tfoot id='L5bHB'></tfoot><dl id='L5bHB'><fieldset id='L5bHB'></fieldset></dl></div>
              <legend id='L5bHB'><style id='L5bHB'><dir id='L5bHB'><q id='L5bHB'></q></dir></style></legend>
                • <bdo id='L5bHB'></bdo><ul id='L5bHB'></ul>
                  本文介绍了POST 请求的图像响应中的奇怪字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  我已经尝试了

                  如果我使用带有 new Image()FileReader() 的静态图像 URL 而不是 POST 请求,它可以正常工作.如何渲染来自 POST 请求的响应的图像?任何帮助表示赞赏.

                  解决方案

                  fotinakis 提供的答案-parse-into-base64-string-the-binary-image-from-response">如何将响应中的二进制图像解析为base64字符串?成功了.

                  在自己发出 POST 请求时,responseType 必须设置为 arraybuffer,稍后可以将其转换为 base64.从二进制文件中获取 base64 是问题所在.ajax 的成功回调默认返回二进制,除非另有设置.

                  其他 responseType 可以在 https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType.

                  我还是不明白一件事.静态图像 URL(GET) 还返回二进制数据,这对 new Image() 没有任何问题,但如果我从 POST 请求成功回调中传递相同的二进制数据,它就不起作用.

                  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)

                  I've tried everything mentioned in AJAX - Weird characters in img tag after request response

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

                  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.

                  解决方案

                  The answer provided by fotinakis in How to parse into base64 string the binary image from response? worked.

                  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.

                  Other responseTypes can be found in https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType.

                  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 请求的图像响应中的奇怪字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                  How to show a running progress bar while page is loading(如何在页面加载时显示正在运行的进度条)
                  HTML5 Canvas toDataURL returns blank(HTML5 Canvas toDataURL 返回空白)
                  How to fix getImageData() error The canvas has been tainted by cross-origin data?(如何修复 getImageData() 错误画布已被跨域数据污染?)
                  Html5 canvas drawImage: how to apply antialiasing(Html5画布drawImage:如何应用抗锯齿)

                  <small id='usm4r'></small><noframes id='usm4r'>

                    <tfoot id='usm4r'></tfoot>
                        <tbody id='usm4r'></tbody>
                    1. <legend id='usm4r'><style id='usm4r'><dir id='usm4r'><q id='usm4r'></q></dir></style></legend>

                          <i id='usm4r'><tr id='usm4r'><dt id='usm4r'><q id='usm4r'><span id='usm4r'><b id='usm4r'><form id='usm4r'><ins id='usm4r'></ins><ul id='usm4r'></ul><sub id='usm4r'></sub></form><legend id='usm4r'></legend><bdo id='usm4r'><pre id='usm4r'><center id='usm4r'></center></pre></bdo></b><th id='usm4r'></th></span></q></dt></tr></i><div id='usm4r'><tfoot id='usm4r'></tfoot><dl id='usm4r'><fieldset id='usm4r'></fieldset></dl></div>
                            <bdo id='usm4r'></bdo><ul id='usm4r'></ul>