1

I need to use an AJAX request to load a gif image from this source:

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:

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:

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);
});
7
  • 1
    It won't work! isn't a proper problem description and is fairly meaningless. There is no datatype: "image/gif" for ajax and what are you doing with return? Show the full usage of this code Commented Sep 7, 2015 at 20:57
  • did u check whether the success callback is getting executed or not..?? like some console log or something...!!! Commented Sep 7, 2015 at 21:11
  • Yes I did. The problem is that the image string is corrupted or truncated, I need to figure out how to properly encode the image string Commented Sep 7, 2015 at 21:12
  • but you said if you directly place the returned image string as image src that is working...?? Commented Sep 7, 2015 at 21:15
  • 1
    similar stackoverflow.com/questions/17657184/… Commented Sep 7, 2015 at 21:57

2 Answers 2

1

You don't need to use Ajax on this particular task. 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]));
        }
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

I know I don't, but I need to solve this problem using AJAX. Also, I've already tried btoa(data) and it still won't work.
Edited my solution. The btoa function will work only on strings. This will work only in modern browsers and again, the Ajax is superfluous. You can use directly the FileReader. Check developer.mozilla.org/en/docs/Web/API/FileReader and.. i've found this duplicate: stackoverflow.com/questions/10982712/…
The only problem is r is going to be text when you need it to be binary
0

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.

How about setting the image src to edgecats.net

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

1 Comment

I do need to use Ajax. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.