简体   繁体   English

如何通过ajax jquery获取图片?

[英]How to get Image through ajax jquery?

I want to display image by ajax jquery in this way so is that possible?我想以这种方式通过 ajax jquery 显示图像,这可能吗?

$.ajax({  
    url: "c.jpeg",
    type :"get",
    dataType: "image/jpeg",

    success: function(data)
    {

      $("#myimg2").attr("src",data);
    }

   })

and this is the html这是 html

<body>
<img id="myimg2" ><br>
</body>

If that could be possible in some way.如果这在某种程度上是可能的。 I want also to get the image from php file rather than directly jpeg file using this header.我还想从 php 文件而不是直接使用此标头的 jpeg 文件获取图像。

header('content-type: image/jpeg');

Please, try this.请试试这个。 It works for me.这个对我有用。

 $.ajax({ url: "path/to/img.jpg", xhrFields: { responseType: 'blob' }, success (data) { const url = window.URL || window.webkitURL; const src = url.createObjectURL(data); $('#image').attr('src', src); } });
 <div> <img id="image"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM