如何显示我们通过Ajax调用收到的图像? [英] How to display an image that we received through Ajax call?

查看:40
本文介绍了如何显示我们通过Ajax调用收到的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Web服务器生成图像并直接将它们发送给客户端。出于安全原因,图像没有URL。例如,如果我输入 / images / 25 浏览器服务器中的URL将发送它,浏览器将下载它。



现在我想从Ajax调用中获取此图像,然后将其显示在现有页面上。我可以得到图像数据。我的问题是:如何显示图像?

  $。get(/ images / 25,function(rawImageData) {
// ???需要将图像添加到DOM
});

更新



<我很抱歉这么傻。谢谢你,JW。当然我可以将带有src的img标签放到我的URL中。无论这是图像文件的直接URL还是服务器动态发送它都没关系。

解决方案

所以听起来像一个网址,它是 / images / 25



至于我知道,您无法显示从AJAX调用中获得的图像数据*。但为什么不把URL放在< img> 标签中呢?浏览器生成的图像与服务器生成无关,而不是从文件中读取。



* 编辑:我错了;如果你真的需要使用AJAX调用,请参阅gideon的答案(例如,你需要发出POST请求,或发送某些标题)。


Web server generates images and sends them to client directly. There are no URLs to the images, for security reasons. For example, if I enter /images/25 URL in the browser server will send it and browser will download it.

Now I want to get this image from Ajax call and then display it on existing page. I can get the image data. My question is: how can I display an image?

$.get("/images/25", function (rawImageData) {
   // ??? Need to add an image to DOM
});

Update

I apologize for being so stupid. Thank you, JW. Of course I can just put img tag with src to my URL. It does not matter if this is a direct URL to an image file or the server sends it dynamically.

解决方案

So it sounds like there is a URL, and it's /images/25.

As far as I know, you can't display image data that you get from an AJAX call*. But why not just put the URL in an <img> tag? It doesn't matter to the browser that the image is generated by the server, rather than read from a file.

*EDIT: I was wrong; see gideon's answer if you really need to use an AJAX call (e.g. you need to make a POST request, or send certain headers).

这篇关于如何显示我们通过Ajax调用收到的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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