播放:未设置会话Cookie。无法使用Javascript读取Cookie

时间:2022-08-23 10:33:27

In order to send an authorization token (that should be stored in a database) to my server, I do an AJAX call in my .scala.html file.

为了将授权令牌(应该存储在数据库中)发送到我的服务器,我在.scala.html文件中进行了一次AJAX调用。

$.ajax({
       url: "http://localhost:9000/storeauthcode",
       type: 'get',
       data: {code: authResult['code']},
       contentType: 'application/json',
       dataType: 'json',
       success: function(result) {
       // Handle or verify the server response.
           console.log("you're fully logged in now.")
           console.log(JSON.stringify(result, null, 2))
           console.log("document.cookie = "+document.cookie)
       },
       error: function (xhr, status, error) {
           console.log("error\n")
           console.log(xhr.responseText)
       }

   });
  }

On the server side, I store the auth code and return a json response

在服务器端,我存储auth代码并返回json响应

  def storeAuthCode  = Action { request =>
  //store credentials in database
  //...
  //return some data
  Ok(Json.toJson(Map("a"->"b"))).withSession("code"-> "someAuthCode", "id"->"someId")
}

If I try to print all cookies in the success handler of my AJAX call via

如果我尝试通过我的AJAX调用的成功处理程序打印所有cookie

console.log("document.cookie = "+document.cookie)

document.cookie seems to be empty although the server should have created a session Cookie (or at least anything). document.cookie should return a ";" separated list of my Cookie values.

document.cookie似乎是空的,虽然服务器应该创建一个会话Cookie(或至少任何东西)。 document.cookie应该返回一个“;”我的Cookie值的分隔列表。

How can I set my Cookies successfully?

如何成功设置Cookies?

1 个解决方案

#1


The reason why you can't read this is HTTPOnly flag set for Play's session cookie.

您无法读取此内容的原因是为Play的会话cookie设置了HTTPOnly标志。

You have several options which you can use instead:

您可以使用几个选项:

  1. Send code and id in the JSON object (like "a"->"b")
  2. 在JSON对象中发送代码和id(如“a” - >“b”)

  3. You can also send them as response headers so you can get them within your success callback like:

    您也可以将它们作为响应标头发送,以便您可以在成功回调中获取它们,如:

    Ok(Json.toJson(Map("a" -> "b")))
      .withHeaders(
        "code" -> "foo",
        "id" -> "bar"
      )
      .withCookies(
        Cookie("code", "foo"),
        Cookie("id", "bar")
      )
    

    jQuery

    success: function(data, textStatus, jqXHR) {
        console.log(jqXHR.getResponseHeader('code'));
        console.log(jqXHR.getResponseHeader('id'));
    },
    

#1


The reason why you can't read this is HTTPOnly flag set for Play's session cookie.

您无法读取此内容的原因是为Play的会话cookie设置了HTTPOnly标志。

You have several options which you can use instead:

您可以使用几个选项:

  1. Send code and id in the JSON object (like "a"->"b")
  2. 在JSON对象中发送代码和id(如“a” - >“b”)

  3. You can also send them as response headers so you can get them within your success callback like:

    您也可以将它们作为响应标头发送,以便您可以在成功回调中获取它们,如:

    Ok(Json.toJson(Map("a" -> "b")))
      .withHeaders(
        "code" -> "foo",
        "id" -> "bar"
      )
      .withCookies(
        Cookie("code", "foo"),
        Cookie("id", "bar")
      )
    

    jQuery

    success: function(data, textStatus, jqXHR) {
        console.log(jqXHR.getResponseHeader('code'));
        console.log(jqXHR.getResponseHeader('id'));
    },