[html] post 방식을 링크 시키는 방법

1. HTML의 form 이용

<form name="myForm" action="http://httpbin.org/post" method="POST">
<input type="hidden" name="some_key1" value="some_value1" />
<input type="hidden" name="some_key2" value="some_value2" />
</form>

<a href="#" onclick="javascript:document.myForm.submit();">click</a>

click




2. JavaScript 함수 이용

<script>
    function page_move(url, some_data) {
        var form = document.createElement("form");
        var parm = new Array();
        var input = new Array();

        form.action = url;
        form.method = "post";


        parm.push( ['some_key1', 'some_value1'] );
        parm.push( ['some_key2', 'some_value2'] );
        parm.push( ['some_data', some_data] );


        for (var i = 0; i < parm.length; i++) {
            input[i] = document.createElement("input");
            input[i].setAttribute("type", "hidden");
            input[i].setAttribute('name', parm[i][0]);
            input[i].setAttribute("value", parm[i][1]);
            form.appendChild(input[i]);
        }
        document.body.appendChild(form);
        form.submit();
    }
</script>

<a href="#" onclick="javascript:page_move('http://httpbin.org/post', 'foobar');">click</a>

click


  1. Favicon of http://nazuna.kr BlogIcon 나즈나 2018.07.25 04:26 신고

    두번째 방법 document.body.appendChild(form); 빠졌네요..

+ Recent posts