gpt4 book ai didi

javascript - JS_of_ocaml - 获取 cookie 时出错

转载 作者:行者123 更新时间:2023-11-28 00:29:24 25 4
gpt4 key购买 nike

我是XmlHttpRequest,为了发出http请求,我想获取cookie。获取cookie的代码:

let http_post url =
XmlHttpRequest.perform_raw_url
~post_args:[("login", `String (Js.string "foo"));
("password", `String (Js.string "bar"))]
url >>= fun r ->
let code = r.XmlHttpRequest.code in
let msg = r.XmlHttpRequest.content in
let cookie = match r.XmlHttpRequest.headers "Set-Cookie" with
| None -> "Empty Cookie"
| Some s -> s in
if code = 0 || code = 200
then Lwt.return (msg,cookie)

let make_test_request id =
let button = get_element_by_id id in
button##onclick <- (Html.handler (fun _ ->
http_post "www.website.com" >>=
(fun (msg,cookie) ->
Printf.printf "cookie = %s\n" cookie;
Html.document##cookie <- Js.string cookie;
Printf.printf "s = %s\n" msg;
Lwt.return());
Js._true))

Cookie 应该位于 header 中,但我收到此错误:拒绝获取不安全的 header “Set-Cookie”

这是我获取 cookie 错误的方式还是我的网络浏览器有问题(我使用的是 chromium)?

最佳答案

http://www.w3.org/TR/XMLHttpRequest/#the-getresponseheader()-method

client . getResponseHeader(header) Returns the header field value from the response of which the field name matches header, unless the field name is Set-Cookie or Set-Cookie2.

答案为Why cookies and set-cookie headers can't be set while making xmlhttprequest using setRequestHeader?

关于javascript - JS_of_ocaml - 获取 cookie 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29154823/

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