0

我创建了一个使用会话发送视频 ID、标题等的 YouTube 搜索引擎。我为每个按钮创建了具有唯一 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 调用会话函数并将值传递给它。而且,它应该覆盖会话值。但在这种情况下,它不会覆盖这些值。我该如何克服这个问题?有什么建议么?

4

3 回答 3

0

请求脚本时是否设置了 GET-Values?使用您的 Firebug 来检查 PHP 脚本所回答的内容。(网络 -> 当您的 Ajax 请求启动时,您将看到正在加载的请求文件)。

于 2013-07-23T08:38:47.317 回答
0

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

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

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

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

在一个侧面节点上:为什么要为此使用 ajax?您可以在转发时简单地传递附加到 url 的参数...?

于 2013-07-23T10:41:01.213 回答
0

收到 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 然后更改实际页面的位置的意义。也许您应该在另一个窗口中开始下载?

于 2013-07-23T14:16:11.297 回答