【问题标题】:Js_of_ocaml - Error when get cookieJs_of_ocaml - 获取 cookie 时出错
【发布时间】:2015-05-23 04:06:45
【问题描述】:

我是 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 应该在标头中,我收到此错误:拒绝获取不安全的标头“Set-Cookie”

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

【问题讨论】:

    标签: javascript cookies js-of-ocaml


    【解决方案1】:

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

    客户端。 getResponseHeader(header) 从以下位置返回标头字段值 字段名称与标头匹配的响应,除非该字段 名称是 Set-Cookie 或 Set-Cookie2。

    回复Why cookies and set-cookie headers can't be set while making xmlhttprequest using setRequestHeader?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-18
      • 2020-01-07
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多