简体   繁体   English

ajax调用后更新图像

[英]update image after ajax call

I have some problems. 我有一些问题。

On click I rotate image with ajax and save new image. 点击我用ajax旋转图像并保存新图像。 Everything is without page refresh. 一切都没有页面刷新。 And problem is that old image won't refresh and put new refreshed image. 问题是旧图像不会刷新并刷新新图像。

Also new image have same name like the old one. 此外,新图像与旧图像具有相同的名称。

QUESTION

My question is how to update img from folder without page refresh? 我的问题是如何在没有页面刷新的情况下从文件夹更新img? Only after click class slika-rotiraj. 只有在点击类slika-rotiraj之后。

HTML : HTML:

<div class="slika">
    <div class="slika-obrisi"></div>
    <div class="slika-rotiraj"></div>
    <img src="uploads/87b6f334f30717343acd69f3962142a0_542ad975e424d.jpg" />
</div>

SCRIPT : 脚本:

$(document).on("click", ".slika-rotiraj", function() {
    var slika = $(this).next("img").attr("src");
    var slika1 = $(this).parent();
    var slika2 = $(this).next("img");
    $(this).next("img").attr("src", slika);
    $.ajax({
        type: "POST",
        url: "rotiraj.php",
        data: {
            slika: slika
        },
        success: function(data) {

        }
    });
});

Do you mean that the image has changed server-side and you just want to force the img tag to fetch the new state of the image? 你的意思是图像已经改变了服务器端 ,你只是想强制img标签来获取图像的新状态? Simply setting the src should accomplish that: 只需设置src可以实现:

success: function(data) {
    // which is your image?  "slika2"?
    // it's not really obvious, so I'll assume that for now
    $(slika2).attr('src', slika);
}

If this isn't refreshing the image, you might add a "cache-breaker" to force it to re-request. 如果这不刷新图像,您可以添加“缓存破坏程序”以强制它重新请求。 Something as simple as this: 像这样简单:

success: function(data) {
    $(slika2).attr('src', slika + '?' + new Date().getTime());
}

This wouldn't affect anything, it would just innocuously change the URL and force the browser to re-request from the server. 这不会影响任何事情,它只是无害地更改URL并强制浏览器从服务器重新请求。

Give your tag an id and set the value of the 'src' attribute inside your success function of the ajax call. 为您的标记指定一个id,并在ajax调用的success函数中设置'src'属性的值。

HTML HTML

<div class="slika">
    <div class="slika-obrisi"></div>
    <div class="slika-rotiraj"></div>
    <img id="myimage" src="uploads/87b6f334f30717343acd69f3962142a0_542ad975e424d.jpg" />
</div>

JS: JS:

$(document).on("click", ".slika-rotiraj", function() {
    var slika = $(this).next("img").attr("src");
    var slika1 = $(this).parent();
    var slika2 = $(this).next("img");
    $(this).next("img").attr("src", slika);
    $.ajax({
        type: "POST",
        url: "rotiraj.php",
        data: {
            slika: slika
        },
        success: function(data) {
            $("#myimage").attr('src', data);
        }
    });
});

Try something like this if the image has always the same name: 如果图像始终具有相同的名称,请尝试这样的操作:

$('.slika img').attr('src', 'new path to image');

otherwise: 除此以外:

$('.slika img').attr('src', 'new path to image' + '?_=' + new Date().getTime()); 

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

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