简体   繁体   English

使用jQuery AJAX显示图像

[英]Image display using jQuery AJAX

I am using JavaScript with jQuery AJAX and I have script as below; 我将JavaScript与jQuery AJAX结合使用,并且脚本如下:

$(document).ready( function() {
$('#process').click(function() {
  var $process = $(this);
  var $message = $process.parent().parent().next().children('td');
  $message.text("Processing...");
  $.ajax({
    type: 'POST',
    url: 'myajaxfile.php',
    data: 'data1=flename1&data2=2&data3=mix',
    dataType: 'json',
    cache: false,
    success: function(result) {
      var result1 = result[0];
      var result2 = result[1];
      if(result1 == "true"){
        alert("true");
        $('#preview').css({ 'background-image':'url(' + result2 + ')' });
        $message.text("");
        return(false);
      }else{
        alert("false");
        $message.html("Error !<br>Error !");
      }
    },
  });
});
});

This sends some data to my ajax file and changes background image of my div having id preview . 这会将一些数据发送到我的ajax文件,并更改具有id preview div的背景图像。 Everything works well. 一切正常。 But still some confusing problem. 但是仍然存在一些令人困惑的问题。 The ajax file first make some thumbnail image according to data send where data1 = filename1, data1 = filename2 etc etc. If thumbnail is made, it will return a json_encoded array with result1 = true and result2 = imagename . 所述AJAX文件首先要根据一些数据的缩略图图像发送其中DATA1 =文件名1,数据1 =文件名2等等。如果缩略图制成,它将返回一个json_encoded arrayresult1 = trueresult2 = imagename The imagename variable contains full path to the image, images/img1.jpg . imagename变量包含图像的完整路径images/img1.jpg But the preview is a headache. 但是预览令人头疼。 Suppose, if it send request with data1=flename1 , it will create a thumbnail img1.jpg and is correctly displayed. 假设,如果它发送带有data1=flename1请求,它将创建一个缩略图img1.jpg并正确显示。 Then i changed data1 to data2, refreshed page and sent request. 然后我将data1更改为data2,刷新了页面并发送了请求。 but the image displayed was the previous image. 但显示的图像是前一张图像。 I checked directory and found each time for the request, a corresponding correct image file is created in directory. 我检查了目录,并发现每次请求都在目录中创建了一个相应的正确映像文件。 but the problem is with the display. 但是问题出在显示器上。 Also, the correct image is getting displayed if I repeat Ajax request for 2 or more times after refreshing the page. 另外,如果刷新页面后我重复Ajax请求两次或两次以上,则将显示正确的图像。 it is because of the same image name. 这是因为图像名称相同。 but I have no other choice. 但我别无选择。 I am using Firefox. 我正在使用Firefox。

How can I solve this? 我该如何解决?

Sounds like you have a caching problem. 听起来您有缓存问题。 The first image is fetched from the server but when the images without the path changing, the browser will pull the image out of its cache and you'll see the old one even though the server has the new one. 第一个图像是从服务器获取的,但是当图像没有更改路径时,浏览器会将图像从其缓存中拉出,即使服务器具有新的图像,您也会看到旧的图像。

The simplest and most reliable way I've found for cache-busting is to append a random number in a CGI parameter to the image's URL: 我发现用于缓存清除的最简单,最可靠的方法是在CGI参数中向图像的URL附加一个随机数:

$('#preview').css({
    'background-image': 'url(' + result2 + '?cb=' + Math.random() + ')'
});

You might need to adjust your server configuration to ignore the ?cb but it should do that on its own. 您可能需要调整服务器配置以忽略?cb但是它应该自己执行。

You could also try playing with the Cache-Control HTTP header on your server for the images in question. 您也可以尝试使用服务器上的Cache-Control HTTP标头来处理有问题的图像。 Setting: 设置:

Cache-Control: no-cache

should do it but the cache-busting URL kludge is more fool proof. 应该做到这一点,但是缓存无效的URL垃圾更加可靠。 Including the Cache-Control header for temporary images is still a good idea though. 不过,包括临时图像的Cache-Control标头仍然是一个好主意。

Have you tried checked what the URL is that returned by yours ajax call? 您是否尝试检查过您的Ajax调用返回的URL是什么? Maybe your server side script is not correct: 也许您的服务器端脚本不正确:

var result1 = result[0];       
var result2 = result[1];     

alert(result2); // should help you determine if the script is returning the correct image path

if(result1 == "true"){
... 

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

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