프로그래밍 정리/Front

jsp내에서 post보내기(가상 폼 만들기)

Wooni0477 2020. 3. 2. 16:37
반응형
jsp에서 post 보내기가 까다롭다.

그래서 가상폼을 만들어 손쉽게 보내는 방법을 살펴보자

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
<script>
//가상 폼
function ComSubmit(opt_formId) {
 
    //null 검사
    this.formId = cfn_isNull(opt_formId) == true ? "commonForm" : opt_formId;
    this.url = "";
      
    if(this.formId == "commonForm"){
        $("#commonForm")[0].reset();
    }
      
    this.setUrl = function setUrl(url){
        this.url = url;
    };
      
    this.addParam = function addParam(key, value){
        $("#"+this.formId).append($("<input type='hidden' name='"+key+"' id='"+key+"' value='"+value+"' >"));
    };
      
    this.submit = function submit(){
        var frm = $("#"+this.formId)[0];
        frm.action = this.url;
        frm.method = "post";
        frm.submit();  
    };
}
//null 검사
function cfn_isNull(sParam){
    if(typeof(sParam) == "number" || typeof(sParam) == "boolean")
        return false;
    if(sParam == '' || sParam == null || sParam == undefined || sParam == 'undefined'){
        return true;
    }else{
        return false;
    }
}
 
//사용할때 변경해야할 부분
$("#text").on("click",function(){
    var comSubmit = new ComSubmit();
 
    comSubmit.setUrl("<c:url value='/###/####.do' />");
    comSubmit.addParam("key",$("#id값").val(););
    comSubmit.submit();
});
 
</script>
</head>
<body>
   <body>
  <!-- 가상폼 -->
  <form id="commonForm" name="commonForm"></form> 
  
  <button id="text"></button>
</body>
</body>
</html>
 
cs


반응형