2017-09-20 6 views
0

ユーザーがhrefタグをクリックすると、動的フォームを作成して送信しています。ここで動的フォームjqueryでのCookieの送信

コード

<script> 
     $(document).ready(function() { 
     $("#logout").click(function() { 
      var form = document.createElement('form'); 
      form.setAttribute('action', '${pageContext.request.contextPath}/logout/user'); 
      form.setAttribute('method', 'POST'); 
      document.body.appendChild(form); 
      form.submit(); 
     }); 

     }); 

    </script> 

問題は、私はまた、ユーザーが自分のアプリケーションにログインしたときに、以前に設定されたクッキーを提出しなければならない、ということです。現在、バックエンドで私はクッキーを取得していません。

クッキーの送信方法。

+1

クッキーを明示的に提出しないと、ブラウザはあなたにそれを行います。クッキーを取得していない場合は、最初にクライアント側に設定されていない可能性が高く、フォームを送信するパスには有効ではありません。 – CBroe

+0

@CBroeはいあなたは正しいです。私はクロムの設定をチェックし、私のクッキーはパス/ sayd/loginに設定されています。私はパス/ sayd/logoutに公開されている私のコントローラにアクセスしようとしています。だから私はこれをどう扱うべきですか? –

+0

クッキーを設定するときにパスを '/ sayd /'として指定する必要があります。 – CBroe

答えて

0
You can then do: 

$.cookie("test", 1); 
To delete: 

$.removeCookie("test"); 
Additionally, to set a timeout of a certain number of days (10 here) on the cookie: 

$.cookie("test", 1, { expires : 10 }); 
If the expires option is omitted, then the cookie becomes a session cookie, and is deleted when the browser exits. 

To cover all the options: 

$.cookie("test", 1, { 
    expires : 10,   //expires in 10 days 

    path : '/',   //The value of the path attribute of the cookie 
          //(default: path of page that created the cookie). 

    domain : 'jquery.com', //The value of the domain attribute of the cookie 
          //(default: domain of page that created the cookie). 

    secure : true   //If set to true the secure attribute of the cookie 
          //will be set and the cookie transmission will 
          //require a secure protocol (defaults to false). 
}); 
関連する問題