jquery ajax post onclick - php session variable


jquery ajax post onclick - php session variable

在我的主页上,我正在使用最大全屏图像插件。当有人点击具体链接(产品页面),我必须找出幻灯片中的当前图像是什么并将其设置为产品索引页面中的背景图像。

当有人单击主页上的"/producten"链接并将其存储为会话变量时,我正在进行 ajax 调用。

问题是,它没有进行 ajax 调用,我在 apache 日志文件中看不到 POST 请求,只有"/producten"页面的 GET 请求。会快吗?我不能在发出 GET 请求之前执行 POST 请求吗?我无法确定它。这是我的代码:

首页索引:

jQuery(document).ready(function($)
{
    $("a[href='/producten']").click(function() {
        var best;
        var maxz;
        $('.mc-image').each(function() {
            var z = parseInt($(this).css('z-index'), 10);
            if (!best || maxz<z) {
                best = this;        
                maxz = z;
            }
        });
        var bg_image = $(best).css('background-image');
        bg_image     = bg_image.replace('url(','').replace(')','');
        $.post('/producten', {bg_image:bg_image});
    });
});

bg_image设置正确,我使用 console.log() 对其进行了测试,并得到了输出。

/producten index:

<?php
    session_start();
    $_SESSION['bg_image'] = $_POST['bg_image'];
?>

在javascript中:

/* $.post('/producten', {bg_image:bg_image}); */
this.href = this.href + '?bg_image=' + escape(bg_image)

在 php 中:

if(isset($_GET['bg_image'])) {
  $_SESSION['bg_image'] = $_GET['bg_image'];
  header('Location: /producten');
  exit;
}