简体   繁体   English

使用AJAX请求加载图片无法正常工作

[英]Loading image using AJAX Request won't work

I need to use an AJAX request to load a gif image from this source: 需要使用AJAX请求从此源加载gif图像:

http://edgecats.net http://edgecats.net

Everytime I try to do so and use the response on the image source as: 每次我尝试这样做时,在图像源上使用以下响应:

$('#cat-thumb-1').attr('src', 'data:image/gif;base64,' + data);

It won't work! 它行不通!

When copying the response using firefox's devtools and using: 使用firefox的devtools复制响应并使用以下命令时:

data:image/gif;base64,<PASTE_HERE>

Everything works like a charm! 一切都像一个魅力!

How can I figure out how to turn my response into an image correctly? 如何弄清楚如何将我的回复正确地转换为图片?

This is my AJAX request code: 这是我的AJAX请求代码:

function getCatImg() {
    return $.ajax({
        type: 'GET',
        url: 'http://edgecats.net',
        datatype:"image/gif"
    });
}

getCatImg().success(function(data) {
   $('#cat-thumb-1').attr('src', 'data:image/gif;base64,' + data);
});

You don't need to use Ajax on this particular task. 您无需在此特定任务上使用Ajax。 But, if you insist, you must use a trick to make this work: 但是,如果您坚持要使用,则必须使用技巧来完成这项工作:

$(document).ready(function() {
    $.ajax('http://edgecats.net', {
        success: function(r) {
            var reader = new FileReader();

            reader.onload = function(e) {
                $('img').prop('src', e.target.result);
            };

            reader.readAsDataURL(new Blob([r]));
        }
    });
});

I am not familiar with the cats on the edge, but as I've looked at the site, the response was not base64 encoded, so it won't work that way. 我对边缘的猫不熟悉,但是当我查看该站点时,响应不是以base64编码的,因此无法正常工作。

How about setting the image src to edgecats.net 如何将图像src设置为edgecats.net

$('#cat-thumb-1').attr('src', 'http://edgecats.net');

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

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