为什么我的cookie没有被发送到服务器?

时间:2016-09-03 17:24:59

标签: google-chrome http cookies

当我在Chrome中向我的localhost上的网络服务器发出请求时,标题为:

HTTP/1.1 200 OK
Server: nginx/1.9.12
Date: Sat, 03 Sep 2016 18:44:54 GMT
Content-Type: application/json
Content-Length: 520
Connection: keep-alive
Set-Cookie: session="\350C\243\0153RB`\225M4vyD\317\030"; HttpOnly
Strict-Transport-Security: max-age=31536000

当我向服务器发出后续请求时,标题为:

GET /api/larps HTTP/1.1
Host: 192.168.99.100
Connection: keep-alive
accept: application/json
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
content-type: application/json
Referer: https://192.168.99.100/simple.html
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8

以下是展示此问题的网页:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Test</title>
</head>
<body>
  <script>
    const fetch_get_json = url => _fetch(url, 'GET');
    const fetch_post_json = (url, data) => _fetch(url, 'POST', data);

    const _fetch = (url, method, data) => {
      console.log('fetching', url)
      const params = {
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        method
      }

      if (data) {
        params.body = JSON.stringify(data);
      };

      return fetch(url, params);
    }

    fetch_post_json('/api/login', {email: 'user', 'pwd': 'password'}).then(function() {
      fetch_get_json('/api/info');
    });

  </script>
</body>
</html>

为什么没有发送cookie?

0 个答案:

没有答案