douzhongjian0752 2016-10-04 05:49
浏览 12
已采纳

在ajax成功函数中包含$ variable

I am trying to return an image when Ajax is done, but i don't know how to include a session variable in the response, this is my code:

$(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">')
         }
      });
   });
});

Then, I want show the image $_SESSION["sess_img"]; within the div #new-image

  • 写回答

1条回答 默认 最新

  • doutun9179 2016-10-04 05:57
    关注

    You can echo that image filename in process.php like this:

    <?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();
    

    And in your javascript code:

    $(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'));
          }
        }); 
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错