gpt4 book ai didi

javascript - xhr.send 和 php session 值 - 语法正确吗?

转载 作者:行者123 更新时间:2023-11-28 06:31:05 25 4
gpt4 key购买 nike

请使用 xhr.send 发送 php session 数据的正确语法是什么,我的 javascript 知识有限,我设置了如下值:

var csrf_token_value = '<?php echo $_SESSION['csrf_token']; ?>';

这正确设置了 csrf_token_vale

不确定 xhr.send 的语法是否正确,以下内容不会输出 session 值,它只会输出“csrf_token_value”

xhr.send("csrf_token=" + csrf_token_value);

谢谢

最佳答案

这取决于您的请求方式。如果您使用 GET,则必须将 token 作为查询字符串参数放置在 open() 函数中:

xhr.open("GET", "/myendpoint?csrf_token=" + csrf_token_value);

...在发送之前。

如果您使用 POST,那么您将需要一个 FormData 对象:

data = new FormData;
data.set("csrf_token", csrf_token_value);

然后将其与您的请求一起发送:

xhr.send(form);

关于javascript - xhr.send 和 php session 值 - 语法正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34707691/

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