找到你要的答案

Q:Play: Session Cookie not set. Cannot read Cookie using Javascript

Q:会话cookie未设置。读不懂曲奇使用JavaScript

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.

$.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

  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

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.

How can I set my Cookies successfully?

为了发送一个授权令牌(这应该是存储在数据库中)到我的服务器,我在做一个Ajax调用scala.html文件。

$.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)
       }

   });
  }

在服务器端,我店的认证码并返回一个JSON响应

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

如果我要打印所有的饼干在我成功通过Ajax调用处理程序

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

似乎空document.cookie虽然服务器应该已经创建了一个会话cookie(或者至少是什么)。document.cookie应该返回一个“;”隔开我的cookie的值列表。

我怎样才能成功地设置我的cookie?

answer1: 回答1:

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

You have several options which you can use instead:

  1. Send code and id in the JSON object (like "a"->"b")
  2. 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'));
    },
    

你为什么不读这是HTTPOnly的标志设置为播放的会话cookie。

您有几个选项,您可以使用,而不是:

  1. Send code and id in the JSON object (like "a"->"b")
  2. 您也可以发送它们作为响应头,以便您可以让他们在您的成功回调像:

    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'));
    },
    
ajax  scala  session  cookies  playframework