我创建了一个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\" /> Download</button> ";
ajax调用的php页面具有简单的会话创建:
session_start();
$videoid = $_GET['videoid'];
$videotitle = $_GET['videotitle'];
$_SESSION['DownloadID'] = $videoid;
$_SESSION['DownloadTitle'] = $videotitle;
$_SESSION['DownloadType'] = "Download";
我遇到的问题是,当我打开浏览器后第一次点击任何下载按钮时,它运行良好。但是当我再次搜索时,它将返回先前的会话值。我通过ajax调用会话函数并将值传递给它。并且,它应该覆盖会话值。但在这种情况下,它不会覆盖这些值。我怎样才能克服这个问题?有什么建议吗?
答案 0 :(得分:0)
请求脚本时是否设置了GET值? 使用您的Firebug来检查PHP脚本的答案。 (网络 - &gt;您将在启动Ajax请求时看到正在加载的请求文件)。
答案 1 :(得分:0)
目前
xmlhttp.open("GET", "GenerateDownloadSession.php?videoid=" + videoid + "&videotitle=" + videotitle, true);
xmlhttp.send();
您正在执行异步请求,这意味着,在发送请求之后javascript仍在继续,而不是等待它完成。很可能它会将您当前页面的重定向发送到下载,之后调用可能会更改会话数据。
解决方案1.)通过将最后一个参数设置为false来使脚本同步。
解决方案2.)将前向逻辑移入onreadystatechange
回调函数。
:为什么要使用ajax?您可以在转发时简单地传递附加到网址的参数...?
答案 2 :(得分: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然后改变实际页面位置的重点。 也许你应该在另一个窗口开始下载?