2 개의 쿠키 값이 있다고 가정합니다. 나는 쿠키 객체의 문법이 아주 단순하지 않고 변수로 쉽게 설정 될 수 없다는 것을 알고있다. 그래서 2 쿠키 2 개 변수를 반환하려면 다음 코드를 사용했습니다 :쿠키를 Json 객체로 변환하는 방법은 무엇입니까?
cookie_name = "username";
cookie_name2 = "password";
var YouWrote=getName();
var YouWrote2=getName2();
function getName() {
if(document.cookie)
{
index = document.cookie.indexOf(cookie_name);
if (index != -1)
{
namestart = (document.cookie.indexOf("=", index) + 1);
nameend = document.cookie.indexOf(";", index);
if (nameend == -1) {nameend = document.cookie.length;}
YouWrote = document.cookie.substring(namestart, nameend);
return YouWrote;
}}}
function getName2() {
if(document.cookie)
{
index = document.cookie.indexOf(cookie_name2);
if (index != -1)
{
namestart = (document.cookie.indexOf("=", index) + 1);
nameend = document.cookie.indexOf(";", index);
if (nameend == -1) {nameend = document.cookie.length;}
YouWrote2 = document.cookie.substring(namestart, nameend);
return YouWrote2;
}}}
지금 YouWrote 및 YouWrote2는 (브라우저에 저장되어있는 것과 동일한) 쿠키 값이 포함되어 있습니다. 이제 Javascript 또는 Jquery를 사용하여 두 개의 YouWrote 변수를 JSON 객체에 저장하고 Post 메소드를 통해 보낼 수 있는지 궁금합니다. 데이터 부분에 빨려
$(document).ready(function() {
$.ajax
({
type: "POST",
url: "Insert URL Here",
dataType: 'json',
async: false,
data: ??????????????????????????
});
:
나는의 라인을 따라 뭔가를 생각하고 있어요. 이것이 올바른 방법인지 확실하지 않습니다.
메신저를 사용하지 않고 있습니다. 하지만 내 API 서버는 내가 전송 한 Json 객체에 응답 할 수 있습니다. 하지만 도움을 주셔서 감사합니다. – lancethestudent
쿠키를 사용하여 저장하는 것이 좋습니다. 세션이 완료되면 쿠키는 만료됩니다. – lancethestudent