在PHP中通过jquery发出ajax请求时维护$_SESSION数据?
我正处于一个不错的项目的最后阶段(至少对我来说),但我遇到了一个问题,我的 ajax 请求没有将 $_SESSION 数据发送到它们正在加载的 URL。我正在尝试使用 Gdata 自动将文件上传到 YouTube。我使用 jquery 发出 ajax 请求,但是当我在名为 PHP 的 ajax 脚本中测试 $_SESSION['sessionToken'] 时,我什么也没得到。
这是我的代码,它调用 YouTube 上传脚本:
function uploadVideos(id, upload, selected) {
var status = document.getElementById('currentStatus');
var fields = upload[id].split(":", 2);
var token="<?php echo $_GET['token'];?>";
var dataString = 'videoId='+ fields[0]; // + '&token=' + token;
id++;
status.innerHTML = "<b>Videos for Upload:</b> <h3 class='status'>Currently Updating Bout #" + fields[1] + " (" + id + " of " + upload.length + " videos)</h3>";
$.ajax({
type: "GET",
url: "upload.php",
data: dataString,
success: function(txt) {
if (txt != 'Success') {
alert(txt);
}
if (id < upload.length) {
uploadVideos(id, upload, selected);
} else {
status.innerHTML = "<b>Videos for Upload:</b> <h3 class='status'>Completed</h3>";
}
//deselect the checkbox
document.videos.video[selected[id-1]].checked = false;
document.videos.video[selected[id-1]].style.display = 'none';
},
async: true
});
}
我如何将 sessionToken 发送到 upload.php,让它相信我已经通过身份验证?
I'm on the last leg of a decent project (for me at least) and I'm running into an issue where my ajax requests aren't sending $_SESSION data to the URLs they are loading. I'm trying to automate the upload of files you YouTube, using Gdata. I make an ajax request using jquery but when I test for $_SESSION['sessionToken'] in my ajax called PHP script, I get nothing.
Here's my code, that's calling the YouTube upload script:
function uploadVideos(id, upload, selected) {
var status = document.getElementById('currentStatus');
var fields = upload[id].split(":", 2);
var token="<?php echo $_GET['token'];?>";
var dataString = 'videoId='+ fields[0]; // + '&token=' + token;
id++;
status.innerHTML = "<b>Videos for Upload:</b> <h3 class='status'>Currently Updating Bout #" + fields[1] + " (" + id + " of " + upload.length + " videos)</h3>";
$.ajax({
type: "GET",
url: "upload.php",
data: dataString,
success: function(txt) {
if (txt != 'Success') {
alert(txt);
}
if (id < upload.length) {
uploadVideos(id, upload, selected);
} else {
status.innerHTML = "<b>Videos for Upload:</b> <h3 class='status'>Completed</h3>";
}
//deselect the checkbox
document.videos.video[selected[id-1]].checked = false;
document.videos.video[selected[id-1]].style.display = 'none';
},
async: true
});
}
How can I send sessionToken along to upload.php, to it believes I'm authenticated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一:
您没有通过 Ajax 调用发送“token”变量。您正在发送不包含令牌变量的变量 dataString。您已注释掉令牌部分...
第二:
您只能
在使用 GET 的 ajax 调用中使用 POST 或 GET 发送 Ajax,因此您应该检查 $_GET;不是 $_SESSION。
First:
You are not sending the "token"-variable with your Ajax call. You are sending the variable dataString which does not include the token variable. You have commented the token part out...
Second:
You can only send Ajax with POST or GET
in your ajax call you are using GET, so you should check for $_GET; Not $_SESSION.