본문 바로가기
jQuery

$.getJSON을 post 방식으로 사용하기.

by eqzero 2010. 11. 28.

jQuery의 $.getJSON은 기본적으로 post방식을 지원하지 않는다.
그래서 꽤 긴내용의 파라미터를 보내서 처리를 할경우 난감한 상황을 만나게 된다..;;
그래서 구글링했더니 아래와 같은 꽁수(?)로 해결을 봤다.
결과적으로 $.post를 사용 post방식으로 처리를하지만 결과값을 json형식으로 받는다고 정의를 한후 사용하게 된다.

postJSON이라는 별도의 함수를 만든후 사용하게 된다. 

      $.postJSON = function(url, data, func) { 
             $.post(url+(url.indexOf("?") == -1 ? "?" : "&")+"callback=?", data, func, "json"); 
      }  



아래는 postJSON을 사용한 샘플 소스

<form name="testFrm" id="testFrm" method="get">
      <input type="hidden" name="test1" value="111" />
      <input type="hidden" name="test2" value="222" />
      <input type="hidden" name="test3" value="333" />
</form>     

<script type="text/javascript">
    function getData(){
        var paramData = $("#testFrm").serialize();   
        

        $.postJSON(url, paramData, function (data) { 
            $.each(data, function(index,jsonData){
                //jsonData의 값을 처리.~~
            });
        });
    }
</script>

'jQuery' 카테고리의 다른 글

jQuery 1.5 버젼 업데이트!~  (0) 2011.02.10
jQuery 1.4 버젼업~!  (0) 2010.02.04
AJAX & IE Caching Issues  (0) 2010.02.03
jQuery의 live() 함수.  (0) 2009.08.30
jQuery 성능 조정  (0) 2009.08.14