developer tip

Javascript에서 POSTDATA 경고없이 페이지를 다시로드하려면 어떻게합니까?

optionbox 2020. 11. 19. 08:06
반응형

Javascript에서 POSTDATA 경고없이 페이지를 다시로드하려면 어떻게합니까?


다음을 사용하여 페이지를 다시로드하고 싶습니다.

window.location.reload(true); 

하지만 새로 고침 기능이 이전 POST 양식 데이터를 다시 보내려고하기 때문에 POSTDATA 경고가 표시됩니다. 이 경고없이 내 페이지를 새로 고치려면 어떻게해야합니까?

업데이트 : 나는 프로젝트를 통제 할 수 없습니다! POST 자체를 해결할 수 없습니다!


사용자가 여전히 뒤로 버튼을 누르고 게시물을 다시 제출할 수 있기 때문에 window.locationJavaScript에서 변경하는 것만으로 위험 합니다. 이 경우 예기치 않은 결과 (예 : 중복 구매 ) 가 발생할 수 있습니다 . PRG는 훨씬 더 나은 솔루션입니다.

PRG (Post / Redirect / Get) 패턴 사용

이 문제를 방지하기 위해 많은 웹 응용 프로그램은 PRG 패턴을 사용합니다. 즉, HTML 페이지를 직접 반환하는 대신 POST 작업이 리디렉션 명령을 반환합니다 (HTTP "Location"응답 헤더와 함께 HTTP 303 응답 코드 (때로는 302) 사용). HTTP GET 요청을 사용하여 다른 페이지를로드하도록 브라우저에 지시합니다. 결과 페이지는 예상치 못한 부작용없이 안전하게 북마크하거나 다시로드 할 수 있습니다.


경고 없이는 새로 고칠 수 없습니다. 새로 고침은 브라우저에 마지막 작업을 반복하도록 지시합니다. 마지막 작업을 반복 할 때 데이터를 다시 제출하는 경우 사용자에게 경고할지 여부를 선택하는 것은 브라우저의 몫입니다.

다음을 수행하여 새 세션으로 동일한 페이지로 다시 이동할 수 있습니다.

window.location = window.location.href;

Rex의 솔루션을 사용하여 다시로드되지 않는 앵커 / 해시 URL (# 포함)에 몇 가지 문제가 있습니다.

그래서 마침내 해시 부분을 제거했습니다.

window.location = window.location.href.split("#")[0];

POST 경고를 우회하려면 전체 URL로 페이지를 다시로드해야합니다. 잘 작동합니다.

window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname;

어떻게 window.location.replace (window.location.href);


JavaScript를 사용할 수 있습니다.

window.location.assign(document.URL);

Firefox 및 Chrome에서 나를 위해 일했습니다.

이 링크를 확인하십시오 : http://www.codeproject.com/Tips/433399/PRG-Pattern-Post-Redirect-Get


이것은 효과가 있었다

<button onclick="window.location.href=window.location.href; return false;">Continue</button>

그것이 없이는 작동하지 않은 이유

거짓 반환;
이전에는 양식 제출 버튼으로 처리했습니다. 명시 적으로 false를 반환하면 양식 제출을 수행하지 않고 해당 페이지에 대한 이전 POST의 결과 인 동일한 페이지를 다시로드합니다.


포스트 데이터를 다 마친 단계에 있고 단순히 페이지를 다시보고 싶다면 window.location을 사용하고 쿼리 매개 변수로 임의의 문자열을 추가하여 새 버전을 보장 할 수도 있습니다. 페이지.


Nikl의 버전은 쿼리 매개 변수 가져 오기를 전달하지 않으며 다음 수정 버전을 사용했습니다.

window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname + window.location.search;

또는 제 경우에는 최상위 페이지 / 프레임을 새로 고쳐야했기 때문에 다음 버전을 사용했습니다.

window.top.location.href = window.top.location.protocol +'//'+ window.top.location.host + window.top.location.pathname + window.top.location.search;

<html:form name="Form" type="abc" action="abc.do" method="get" onsubmit="return false;">

method="get" -문제를 해결합니다.

if method="post" 경고 만옵니다.


게시물 제출없이 페이지를 다시로드하는 함수를 작성했으며 해시에서도 작동합니다.

이 작업 reload은 현재 타임 스탬프 (ms)로 값을 업데이트하여 호출 URL에서 GET 매개 변수를 추가 / 수정하여 수행합니다 .

var reload = function () {
    var regex = new RegExp("([?;&])reload[^&;]*[;&]?");
    var query = window.location.href.split('#')[0].replace(regex, "$1").replace(/&$/, '');
    window.location.href =
        (window.location.href.indexOf('?') < 0 ? "?" : query + (query.slice(-1) != "?" ? "&" : ""))
        + "reload=" + new Date().getTime() + window.location.hash;
};

Keep in mind, if you want to trigger this function in a href attribute, implement it this way: href="javascript:reload();void 0;" to make it work, successfully.

The downside of my solution is it will change the URL, so this "reload" is not a real reload, instead it's a load with a different query. Still, it could fit your needs like it does for me.


you are not forced to use javascript to do every thing. Many problems have easy solutions like this one. you can use this tricky anchor:

<a href=".">Continue</a>

of course I know you may need an automatic solution but this will help in many cases.

good luck


using meta refresh in html

<meta http-equiv='refresh' content='1'>

using meta refresh in php

echo "<meta http-equiv='refresh' content='1'>"

Here's a solution that should always work and doesn't remove the hash.

let currentPage = new URL(window.location.href);
currentPage.searchParams.set('r', (+new Date * Math.random()).toString(36).substring(0, 5));
window.location.href = currentPage.href;

If you use GET method instead of POST then we can't the form filed values. If you use window.opener.location.href = window.opener.location.href; then we can fire the db and we can get the value but only thing is the JSP is not refreshing eventhough the scriplet having the form values.

참고URL : https://stackoverflow.com/questions/570015/how-do-i-reload-a-page-without-a-postdata-warning-in-javascript

반응형