리턴 렌더 유형이 json으로 설정된 경우 응답으로 쿠키를 설정할 수 있습니까?렌더링 유형이 "contentType : text/json"일 때 쿠키 설정
표준 렌더링 유형으로 반환하고 나중에 응답 객체에 쿠키를 설정할 수 있으며 이후 요청에서 다시 가져올 수 있습니다. 그러나 반환 값을 json으로 렌더링하는 동안 쿠키를 설정하는 경우 다음 요청 객체에서 쿠키를 다시 가져올 수 없습니다. 여기 무슨 일 이니?
이 두 조치는 'basicForm'이 'submitRegularSubmit'조치에 대한 일반 양식 게시를 수행 할 때 사용자가 제출을 클릭 할 때 예상대로 작동합니다.
// first action set the cookie and second action yields the originally set cookie
def regularAction = {
// using cookie plugin
response.setCookie("username-regular", "regularCookieUser123",604800);
return render(view: "basicForm");
}
// called by form post
def withRegularSubmit = {
def myCookie = request.getCookie("username-regular");
// returns the value 'regularCookieUser123'
return render(view: "resultView");
}
난 그냥 JSON으로 응답에서 반환하기 전에 쿠키를 설정으로 전환
, 나는 다시 게시물 쿠키를하지 않습니다.요청이 포함 된 HTML 문서를 얻어서 시작하는 형태와 문서로드 이벤트가 발생할 때, 다음과 같은 요청은 다음과 같이 jQuery를 함께 자바 스크립트를 통해 호출됩니다
var someUrl = "http://localhost/jsonAction";
$.get(someUrl, function(jsonData) { // do some work with javascript}
컨트롤러 작업 :
// this action is called initially and returns an html doc with a form.
def loadJsonForm = {
return render(view: "jsonForm");
}
// called via javascript when the document load event is fired
def jsonAction = {
response.setCookie("username-json", "jsonCookieUser456",604800); // using cookie plugin
return render(contentType:'text/json') { 'pair'('myKey': "someValue") };
}
// called by form post
def withJsonSubmit = {
def myCookie = request.getCookie("username-json");
// got null value, expecting: jsonCookieUser456
return render(view: "resultView");
}
사용자가 스크립트를 통하지 않고 '제출'버튼을 누르면 데이터가 서버로 반환됩니다. 'withRegularSubmit'과 'withJsonSubmit'을 모두 제출하기 전에 브라우저 (Firefox)에 저장된 쿠키를보고 클라이언트에 도달했음을 알았습니다.