我是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 = %sn" cookie;
Html.document##cookie <- Js.string cookie;
Printf.printf "s = %sn" msg;
Lwt.return());
Js._true))
Cookie应该在标头中,我收到了以下错误:拒绝获取不安全的标头"设置Cookie"
是我把cookie弄错了,还是我的网络浏览器有问题(我使用的是铬)?
http://www.w3.org/TR/XMLHttpRequest/#the-getresponseheader()-方法
客户。getResponseHeader(header)返回字段名称与标头匹配的响应,除非字段name是Set Cookie或Set-Cookie2。
为什么cookie和设置cookie头可以';是否在使用setRequestHeader进行xmlhttprequest时设置?