在PHP中通过jquery发出ajax请求时维护$_SESSION数据?

发布于 2024-12-12 22:38:59 字数 1624 浏览 2 评论 0原文

我正处于一个不错的项目的最后阶段(至少对我来说),但我遇到了一个问题,我的 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>&nbsp;&nbsp;&nbsp; <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>&nbsp;&nbsp;&nbsp; <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2024-12-19 22:38:59

第一:
您没有通过 Ajax 调用发送“token”变量。您正在发送不包含令牌变量的变量 dataString。您已注释掉令牌部分...

var token="<?php echo $_GET['token'];?>"; 
var dataString = 'videoId='+ fields[0]; // + '&token=' + token;

第二:
您只能

在使用 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...

var token="<?php echo $_GET['token'];?>"; 
var dataString = 'videoId='+ fields[0]; // + '&token=' + token;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文