在ajax成功函数

时间:2016-10-04 05:49:58

标签: php jquery ajax

我正在尝试在Ajax完成时返回一个图像,但我不知道如何在响应中包含会话变量,这是我的代码:

$(document).ready(function(){
    $("#process").click(function(){

      $.ajax({
         type: 'POST',
         url: 'process.php',
         success: function (msg) {
          $('#new-image').html('<img src="uploads/img/$_SESSION["sess_img"];" class="img-upload" alt="new image">')
         }
      });
   });
});

然后,我想在div $_SESSION["sess_img"];

中显示图片#new-image

1 个答案:

答案 0 :(得分:4)

您可以在process.php中回显该图像文件名,如下所示:

<?php
// using time() as the cache buster, the image will never be cache by the browser
// to solve it, you need to add condition that will check if the image has change or not.
// or maybe you need to change the filename if it changes without adding a cache buster.
echo $_SESSION['sess_img_chofer']."?v=".time();

在你的javascript代码中:

$(document).ready(function(){
  $("#process").click(function(){

    $.ajax({
      type: 'POST',
      url: 'process.php',
      success: function (image) {
        $('#new-image').html($('<img>').attr('src', 'uploads/img/' + image).attr('class', 'img-upload'));
      }
    }); 
  });
});