使用ajax覆盖会话变量


Overwrite session variable using ajax

我创建了一个YouTube搜索引擎,可以使用会话发送视频id、标题等。我为每个按钮创建了具有唯一id的按钮,通过ajax调用哪个页面,然后使用该按钮的唯一id生成会话。

javascript代码如下:

    <script type="text/javascript">
        function loadXMLSession(videoid, videotitle) {
            var xmlhttp;
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "GenerateDownloadSession.php?videoid=" + videoid + "&videotitle=" + videotitle, true);
            xmlhttp.send();

            //strip off spaces and embed dashes - clean urls
            var downloadtitle = videotitle;
            downloadtitle = downloadtitle.toLowerCase();
            downloadtitle = downloadtitle.replace("-"," ");//strip off dashes with spaces
            downloadtitle = downloadtitle.replace(/ +(?= )/g,'');//replace multiple spaces with one space
            downloadtitle = downloadtitle.replace(/[`~!@#$%^&*()_|+'-=?;:'",.<>'{'}'[']'''/]/gi, '');//strip off all special characters from video title
            downloadtitle = downloadtitle.replace(/ /g,"-");//replace spaces with dashes
            downloadtitle = downloadtitle.replace(/-+$|(-)+/g, '$1');//replace multiple dashes with single dash
            var url = window.location.hostname;
            url = "/development"//only for development phase
            url = url+"/download/"+downloadtitle+".html";
            window.location=url;
        }
    </script>

下载按钮编码如下:

echo "<button id='"downloadbtn'" class='"btn'" value = '"" . $YouTubeVideoID . "'" onclick='"loadXMLSession(this.value,'" . $VideoContent['6'] . "')'"><img src='"" . $HostURLRedirect . "/img/Arrow-Down-icon.png'" alt='"download'" />&nbsp;Download</button>&nbsp;";

ajax调用的php页面具有简单的会话创建:

session_start();
$videoid = $_GET['videoid'];
$videotitle = $_GET['videotitle'];
$_SESSION['DownloadID'] = $videoid;
$_SESSION['DownloadTitle'] = $videotitle;
$_SESSION['DownloadType'] = "Download";

我遇到的问题是,当我打开浏览器后第一次点击下载按钮时,它运行良好。但当我再次搜索时,它会返回以前的会话值。我通过ajax调用会话函数并将值传递给它。它应该覆盖会话值。但在这种情况下,它并没有覆盖这些值。我该如何克服这个问题?有什么建议吗?

xmlhttp.open("GET", "GenerateDownloadSession.php?videoid=" + videoid + "&videotitle=" + videotitle, true);
xmlhttp.send();

您正在执行异步请求,这意味着javascript在发送请求后继续,而不是等待它完成。很可能会在调用更改会话数据之前,将当前页面重定向到下载

解决方案1.)通过将最后一个参数设置为false来使脚本同步。

解决方案2.)将正向逻辑移动到onreadystatechange回调函数中。

关于侧节点:为什么要使用ajax?你可以在转发时简单地传递附加到url的参数。。。?

请求脚本时是否设置了GET值?使用您的Firebug来检查PHP脚本的答案。(Network->Ajax请求启动时,您将看到请求的文件正在加载)。

一旦得到ajax响应,就应该更改页面的位置:

xmlhttp.onreadystatechange = function() 
{
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        var downloadtitle = videotitle;
        downloadtitle = downloadtitle.toLowerCase();
        downloadtitle = downloadtitle.replace("-"," ");//strip off dashes with spaces
        downloadtitle = downloadtitle.replace(/ +(?= )/g,'');//replace multiple spaces with one space
        downloadtitle = downloadtitle.replace(/[`~!@#$%^&*()_|+'-=?;:'",.<>'{'}'[']'''/]/gi, '');//strip off all special characters from video title
        downloadtitle = downloadtitle.replace(/ /g,"-");//replace spaces with dashes
        downloadtitle = downloadtitle.replace(/-+$|(-)+/g, '$1');//replace multiple dashes with single dash
        var url = window.location.hostname;
        url = "/development"//only for development phase
        url = url+"/download/"+downloadtitle+".html";
        window.location=url;
       }
}

但老实说,我不认为使用ajax然后更改实际页面的位置有什么意义。也许你应该在另一个窗口开始下载?