Js_of_ocaml - 获取cookie时出错

时间:2015-03-19 20:55:02

标签: javascript cookies js-of-ocaml

我是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”

这是因为我错误地使用cookies或者我的网页浏览器出现了问题(我使用的是铬)?

1 个答案:

答案 0 :(得分:1)

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

  

客户。 getResponseHeader(header)返回来自的头字段值   除非字段,否则字段名称与标题匹配的响应   name是Set-Cookie或Set-Cookie2。

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

的答案