Use AJAX to display php generated image

前端 未结 6 2038
攒了一身酷
攒了一身酷 2021-01-25 23:44

I have a php script that randomly generates an image. Something like this:



        
6条回答
  •  日久生厌
    2021-01-26 00:03

    I added another answer because I think that none of the previous answers solved the problem. I think, the only thing the OP wanted was to update(!) the image when the button is clicked. So there is no need for an Ajax request, just reload the image. And you can enforce that by appending a random query string to the image's src attribute.

    $('#button').click(function() {
        var $image = $('#image');
        var plainSrc = $image.attr('src').split("?")[0];  // disregard previous query string
        $image.attr('src', plainSrc + "?" + (new Date().getTime()));
    });
    

提交回复
热议问题