JavaScript - Sizuha/devdog GitHub Wiki
document.findElementById('id').innerHTML = '๋ณ๊ฒฝํ ๋ด์ฉ';
document.location = "url...";
var json_str = JSON.stringify(object);
/*
* path : ์ ์ก URL
* params : ์ ์ก ๋ฐ์ดํฐ {'q':'a','s':'b','c':'d'...}์ผ๋ก ๋ฌถ์ด์ ๋ฐฐ์ด ์
๋ ฅ
* method : ์ ์ก ๋ฐฉ์(์๋ต๊ฐ๋ฅ)
*/
function post_to_url(path, params, method) {
method = method || "post"; // Set method to post by default, if not specified.
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
form.submit();
}
์ฃผ์! Django์์๋ ์ด๊ฑธ๋ก POSTํ๋ฉด ์๋๋ค! Django์์๋ ์ด๋ ๊ฒ ํ๋ฉด CSRF ๋ณด์์ค๋ฅ๋ฅผ ๋ฐ์์ํจ๋ค.
Django์์ ์ฌ์ฉํ ๋๋ document.getElementById('form'); ์ผ๋ก ์ด๋ฏธ ์กด์ฌํ๋ ํผ์ ๊ฐ์ ธ๋ค๊ฐ ํ๋๋ฅผ ์ถ๊ฐํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ฉด ๋๋ค.
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<head>
<meta charset="utf-8" />
<base href="http://localhost/" />
<script type="text/javascript">
function setBaseHref(basehref) {
var theBase = document.getElementsByTagName("base");
theBase[0].href = basehref;
}
</script>
</head>