【问题标题】:Cannot get cookie value无法获取 cookie 值
【发布时间】:2020-07-13 15:03:31
【问题描述】:

我有一个脚本,当我按下一个按钮并从用户那里获取一个数字时,它会调用一个提示。该号码存储在 cookie 中。然后该脚本加载一个视图,该视图从 cookie 中获取数字并将其传递给表单。

目前我正在保存 cookie;我可以在浏览器调试器中看到它。但是当我尝试在我的视图中获取 cookie 时,它​​总是返回 null。

我认为请求在更新 cookie 之前发送了一个带有 null 值的 cookie。

JavaScript

$(document).ready(function() {
    $(document).off('click', '.order_button').on('click', '.order_button', function() {
        // $(".on_button").click(function() {
        var amount = prompt("Please enter amount of orders" ,"0")
        if (parseInt(amount) <= 0 /* || amount === null */) {
            alert("Please enter integer greater than 0")
        } else if (parseInt(amount) > 0) {
            document.cookie = "amount=" + amount; //+ ";max-age=60";
            console.log(document.cookie)
            var link = $(this).find('a');
            console.log(link.attr('href'));
            window.location.href=link.attr('href');
            console.log("OK");
        }
        return false;
    });
});

查看

def add_order(request, meal_slug, meal_restaurant):
    print("OK")
    amount = get_server_side_cookie(request, 'amount', '1')
    print(amount)
    //clear_amount_cookie(request)
    if amount is not None:
        meal = Meal.objects.get(
            slug=meal_slug, restaurant_slug=meal_restaurant)
        user = UserProfile.objects.get(user=request.user)
        order = Order.objects.create(
            meal=meal, customer=user, amount=amount, date=datetime.now())
        order.save()
        //clear_amount_cookie(request)
    return redirect('food_cloud:index')

获取cookie函数

def get_server_side_cookie(request, cookie, default_val=None):
    val = request.COOKIES.get(cookie)
    print(val) //This is where I see the null value
    try:
        int(val)
    except (ValueError, TypeError):
        val = default_val
        print(val + " here")
    return val

【问题讨论】:

  • 你能尝试检查cookie是否真的被发送了吗?在 Chrome 网络选项卡中,您可以拦截对服务器的 HTTP 请求。查看您正在生成的请求并尝试找到 Cookie 标头。它的内容是什么?
  • 我刚刚检查过,发送的“金额”cookie 为“空”。会不会是在更新 cookie 之前发送了请求?
  • 意味着你的问题绝对是在 JS 方面。你输入一个console.log(document.cookie)。它说什么?
  • 当我按原样运行它时,我猜它什么也没说,因为页面被链接调用刷新了。但是当我注释掉链接调用时,它显示“金额”具有我使用提示分配的值。

标签: javascript jquery django cookies


【解决方案1】:

我通过实现 jquery.cookies.js 对象解决了这个问题链接:https://github.com/carhartl/jquery-cookie

然后我替换了创建 cookie 的代码。显然 JQuery 不适合直接处理 cookie

更改代码

$(document).ready(function() {
    $(document).off('click', '.order_button').on('click', '.order_button', function() {
        var amount = prompt("Please enter amount of orders" ,"0");
        if (parseInt(amount) <= 0 /* || amount === null */) {
            alert("Please enter integer greater than 0")
        } else if (parseInt(amount) > 0) {
            Cookies.set('amount', amount) //This is the new code for creating cookie
            console.log(document.cookie)
            var link = $(this).find('a');
            console.log(link.attr('href'));
            window.location.href=link.attr('href');
            console.log("OK");
        }
        return false;
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2022-01-12
    • 1970-01-01
    • 2017-04-28
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多