如何使用 $.post 包含在新页面的请求中

发布于 2024-12-05 01:48:55 字数 1661 浏览 0 评论 0原文

我正在研究无 cookie 会话,我不打算通过 URL 传递变量,但我尝试使用 window.name 或隐藏表单。对于 window.name,我会做这样的事情:

$(window).unload(function(){
    if(location.href.indexOf(<my pages base url>) != -1){
    // the user is going to my site
        if($.parseJSON(window.name).previous_site.indexOf(location.protocol + '/' + location.host) != -1){
        // now I know, that user was of my site
            cookie_alternative = new Object();
            cookie_alternative.previous_site = location.href;
            cookie_alternative.session_id = $.parseJSON(window.name).session_id; // this is important
            .....
            window.name = $.toJSON(cookie_alternative);
        }
        else{
        // the user was elsewhere
            cookie_alternative = new Object();
            cookie_alternative.previous_site = location.href;
            cookie_alternative.session_id = <something new>;
            .....
            window.name = $.toJSON(cookie_alternative);
        }
    }
    else{
    //the user goes somewhere else
        window.name = '';
    }
});

这样我就可以通过这种方式跟踪 session_id 。如果我要使用隐藏的输入字段,我想我会做类似的事情。现在,我希望 window.name 中的这个变量 (session_id) 发挥作用,就好像它是 cookies - 用户单击某个链接,这会将他带到我的网站的不同部分 =>请求发送到服务器,服务器响应并显示页面。通过这个请求,我想使用 post 将 session_id 发送到服务器(就好像它是一个 cookie) - 但我不知道如何做到这一点。我正在考虑这样的事情:

$('a').click(function(){
    $.post({
        url: $(this).attr('href'),
        data: $.parseJSON(window.name).session_id,
    })
})

但我认为这行不通,而且正如我所说,我不会使用 URL 重写。其他方法可能是有一些隐藏的表单类型帖子,单击链接设置表单的操作(链接的 href),设置数据:session_id 并触发表单的操作。 有什么想法可以发送帖子来关注下一页的请求吗?或者如何使用隐藏的表单字段来实现同样的事情?任何帮助真的很感激..

I'm working on cookie-free session, I dont intend to pass variables through URL, but I try to use window.name or hidden forms. For window.name, I'd do mosething like this:

$(window).unload(function(){
    if(location.href.indexOf(<my pages base url>) != -1){
    // the user is going to my site
        if($.parseJSON(window.name).previous_site.indexOf(location.protocol + '/' + location.host) != -1){
        // now I know, that user was of my site
            cookie_alternative = new Object();
            cookie_alternative.previous_site = location.href;
            cookie_alternative.session_id = $.parseJSON(window.name).session_id; // this is important
            .....
            window.name = $.toJSON(cookie_alternative);
        }
        else{
        // the user was elsewhere
            cookie_alternative = new Object();
            cookie_alternative.previous_site = location.href;
            cookie_alternative.session_id = <something new>;
            .....
            window.name = $.toJSON(cookie_alternative);
        }
    }
    else{
    //the user goes somewhere else
        window.name = '';
    }
});

So I can track session_id this way. I suppose I'd do something similar to this, if I was to use hidden input field. And now, I want this variable (session_id) from window.name to act as if it was cookies - user clicks on some link, which takes him to different part of my web => the request is sent to the server, server responds and page is displayed. With this request I want to send session_id to server using post (as if it was a cookie)- but I can't figure out, how to do this. I was thitking about something like this:

$('a').click(function(){
    $.post({
        url: $(this).attr('href'),
        data: $.parseJSON(window.name).session_id,
    })
})

but I don't think it's going to work, and as I said, I'm not going to use URL rewriting. Other way to do this would propably be to have some hidden form type post, click on link sets form's action (href of a link), sets data: session_id and triggers form's action.
Any ideas to send post to follow next pages request? Or how to use hidden form field to achieve the same thing? Any help really appreciated..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

软糖 2024-12-12 01:48:55
$('a').click(function(e){

    //Preventing link from triggering
    e.preventDefault();     

    var elObj = $(e.currentTarget);

    $('#secret-hidden-form').attr('action',elObj.attr('href'));

    $('#secret-hidden-form').submit();
    return false;
})

在页面底部:

<?php 
if (isset($_POST['session_id']) {
    $sessId = $_POST['session_id'] 
} else {
    $sessId = uniqid();
}
?>
<form id="secret-hidden-form" method="post">
    <input type="hidden" value="<?php echo $sessId; ?>">
</form>

也许这样的东西会起作用?

$('a').click(function(e){

    //Preventing link from triggering
    e.preventDefault();     

    var elObj = $(e.currentTarget);

    $('#secret-hidden-form').attr('action',elObj.attr('href'));

    $('#secret-hidden-form').submit();
    return false;
})

At the bottom of your page:

<?php 
if (isset($_POST['session_id']) {
    $sessId = $_POST['session_id'] 
} else {
    $sessId = uniqid();
}
?>
<form id="secret-hidden-form" method="post">
    <input type="hidden" value="<?php echo $sessId; ?>">
</form>

Maybe something like this will work?

贩梦商人 2024-12-12 01:48:55

您可以通过 cookie、URL 查询参数或发布表单数据将每个页面请求的数据发送到服务器。

如果要在不使用 Cookie 的情况下维护网站上每个链接的会话,则需要对用户在网站上访问的每个 URL 进行 javascript 覆盖,以将其转换为表单中带有 sessionID 的表单帖子,或者添加带有 sessionID 的查询参数。这有多实用取决于您的网站上有多少地方您必须覆盖某些内容。

如果我要实现没有 cookie 的会话,我会使用 URL 重写(最好是服务器端),因此页面中的每个 URL 要么都经过一个公共 JS 函数,该函数在处理点击之前添加会话 ID,要么只是将 ID 重写为URL,这样正常的点击就可以发生,并且会话将在 URL 中。这允许您保留大部分导航和服务器端逻辑,但将 sessionID 从 cookie 移动到 URL。当然,这不会从一个用户会话持续到下一个用户会话,除非用户将包含 sessionID 的 URL 添加为书签,这样用户每次访问您的站点时都必须重新建立会话。

You can get data to the server with each page request via cookies, URL query parameters or by posting form data.

Maintaining a session across every link on your site without cookies will require a javascript override of every single URL the user visits on your site to either turn it into a form post with sessionID in the form or to add query parameters with sessionID. How practical this is depends upon how many places on your site, you have to override things.

If I were implementing a session without cookies, I'd use URL rewriting (ideally server-side) so every URL in the page either all goes through a common JS function that adds the session ID before processing the click or just rewrites the ID into the URL so the normal click can just happen and it will have the session in the URL. This allows you to preserve most of your navigation and server-side logic, but moves the sessionID from cookie to URL. Of course, this won't be persistent from one user session to the next unless the user bookmarks a URL with the sessionID in it so users will have to re-establish a session everytime they come to your site.

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