简体   繁体   English

Js_of_ocaml-获取Cookie时出错

[英]Js_of_ocaml - Error when get cookie

I'm XmlHttpRequest in order to make http request and I want to get the cookies. 我是XmlHttpRequest以便发出http请求,我想获取cookie。 The code to get 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))

The cookies should be in the headers and I'm getting this error : Refused to get unsafe header "Set-Cookie" Cookie应该在标题中,并且出现此错误: 拒绝获取不安全的标题“ Set-Cookie”

It is the way that I'm getting the cookies wrong or a problem with my web browser (I'm using chromium) ? 这是我弄错Cookie或Web浏览器出现问题的方式(我使用的是铬)?

http://www.w3.org/TR/XMLHttpRequest/#the-getresponseheader()-method 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. getResponseHeader(header)除非字段名称是Set-Cookie或Set-Cookie2,否则从响应中返回标题名称与标题匹配的标题字段值。

answers at Why cookies and set-cookie headers can't be set while making xmlhttprequest using setRequestHeader? 为什么使用setRequestHeader进行xmlhttprequest时不能设置cookie和set-cookie头?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM