gpt4 book ai didi

javascript - Jquery/Javascript - 获取该 session 的请求 URL 列表,就像在浏览器控制台中一样

转载 作者:可可西里 更新时间:2023-11-01 00:44:59 25 4
gpt4 key购买 nike

我需要能够检索显示在浏览器控制台中的请求 URL 列表,即:GET http://mydomain.com/index.php?p=1&curr=GBP&cat=Food。 200。用户可以点击我的应用程序并应用不同的过滤器并滚动浏览页面,我需要某种方式来跟踪这一点,以便我始终知道已经为该用户 session 加载了哪些数据。

我曾考虑过使用 PHPs $_SERVER['REQUEST_URI'] 并将它们保存在一个 session 中,但后来我不知道如何从我的 JQuery 访问这个 session ,因为它的 JQuery 构造网址。

有人知道如何从控制台访问这些数据吗?这可能吗?如果没有,有人可以建议解决方法吗?

到目前为止,我遇到的 PHP/JQuery 困惑情况:

<?php
session_start();
//keep track of requests.
if (!isset($_SESSION['requests'])) {
$_SESSION['requests'] = array();
} else {
if (!in_array( $_SERVER['REQUEST_URI'], $_SESSION['requests'])) {
$_SESSION['requests'][] = $_SERVER['REQUEST_URI'];
}
}
$requests = json_encode($_SESSION['requests']);

print_r($_SESSION['requests']);
print_r($requests); //these both have values
?>

//further down the page is the javascript
$('.filter a').click(function(e) {
var $this = $(this);
var $optionSet = $this.parents('.option-set');
var group = $optionSet.attr('data-filter-group');
filters[ group ] = $this.attr('data-filter-value');
//***more code for filtering etc******/

var paginate_url = $('.paginate a').attr('href');

//THIS IS PART I CANNOT GET WORKING
var visited_urls= <?=$requests?>;
//console.log($.parseJSON(visited_urls));

console.log(visited_urls); //is always empty
var pageno = ''; //somehow check to see if the URL that has been clicked exists int he requests array, if so get the page number and increment.

var next_url = UpdateQueryString(paginate_url, pageno, group, encodeURIComponent(filter_qry));

最佳答案

我不完全确定您要做什么,但我认为您可以跳过 PHP,只使用 JavaScript 和 sessionStorage:https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#sessionStorage或本地存储:https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#localStorage (取决于您希望数据的持久性)

例如,如果我想监听对“a”标签的所有点击并跟踪这些 href 是否已被访问(以及访问了多少次)

$(document).ready(function() {
// store an empty object in session storage when the page loads
sessionStorage.visited = JSON.stringify({});
});

$('a').on('click', function() {
var storage = JSON.parse(sessionStorage.visited),
href = $(this).attr('href');

// when we get a click check to see if this has been clicked before
if (!storage[href]) {
// if not save it and set the count to 1
storage[href] = 1;
} else {
// otherwise increment the count
storage[href]++;
}

sessionStorage.visited = JSON.stringify(storage);
});

如果您想保存来自 ajax 调用的 url,同样的原则适用,但监听 ajaxSuccess 事件并将来自请求的 url 存储在回调中:http://api.jquery.com/ajaxSuccess/

关于javascript - Jquery/Javascript - 获取该 session 的请求 URL 列表,就像在浏览器控制台中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21441952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com