CMS솔루션마켓, 이온디 - 워드프레스, 라이믹스, 카페24, 그누보드, 엑셀

Contents Management System

xe ajax로 글 불러오기

2020년 04월 23일
jQuery.exec_json("board.dispBoardContentView", {
   "mid":"board", "document_srl":"143"
   }, 
   function(data) {
      // $("#dropHere").html(data.oDocument.title);
      $(".drop").html(data.oDocument.title);
      console.log(data);
   }
);

exec_json() : XE에서 Ajax 요청(Request)을 하는 자바스크립트 함수(정보 변경 또는 정보 조회)

XE_ROOT/common/xml_handler.js에 선언되어 있습니다.
exec_json() 함수는 XE에서 ajax기능을 이용함에 있어 module, act를 잘 사용하기 위한 자바스크립트
exec_xml 보다 exec_json을 추천.
exec_xml에서 특정 데이터를 받아올 때 오류가 나지만 exec_json은 그렇지 않다. 또한 exec_json이 사용법이 더 간단하다.


1
2
3
4
5
6
7
8
exec_json (module.act,
                params,
                callback_func
            );
  
module.act : 모듈명.action ( exec_json()은 자바스크립트 내부에서 요청만 할 뿐 실제적인 일은 해당 모듈의 액션(act)을 호출하여 처리 결과를 받는다.)
params : 전달할 변수 json 형식
callback_func : javascript callback 함수 (응답으로 받은 데이터를 처리할 자바스크립트 함수에게 값을 넘겨주는 함수 , javascript callback 함수 첫번째 인수에 모듈에서 $this->add로 보낸 변수를 받음)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script type="text/javascript">
function moveVar(type, module_srl, var_idx) {
   //  전달할 변수 json 형식
    var params = {
        type       : type,
        module_srl : module_srl,
        var_idx    : var_idx
    };
    exec_json('document.procDocumentAdminMoveExtraVar', params, function() { location.reload() }); // 실행
}
</script>
 
그런다음 해당 버튼을 클릭 위 또는 아래로 해서 순서 변경을 하게 됩니다.
<button type="button" class="x_icon-arrow-up" onclick="moveVar('up','123','3')" title=" 위로 ">위로</button>
<button type="button" class="x_icon-arrow-down" onclick="moveVar('down','123','3')" title=" 아래로 ">아래로</button>
이번에는 exec_json()를 이용 해서 특정 게시물 데이타를 출력하는 스크립트 예제입니다.


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
<script type="text/javascript">
// 게시물 조회
function doSelectDocView(document_srl) {
     var params = {
        mid : 'board',
        document_srl : document_srl
    };
    exec_json('board.dispBoardContentView', params, completeDocView);
}
 
// 조회 후 출력
// javascript callback 함수 첫번째 인수에 모듈에서 $this->add로 보낸 변수를 받음
function completeDocView(ret_obj) {
    var error = ret_obj.error;
    var message = ret_obj.message;
    var oDocument = ret_obj.oDocument;
    if(message != 'success') { // 실패시
        alert(message);
    }
 
    var tmp_msg = ""// 게시물 제목
    tmp_msg += "게시판 제목 : "+oDocument.title; // 게시물 제목
    tmp_msg += "<br /><br />게시물 내용<br />"// 게시물 제목
    tmp_msg += oDocument.content; // 게시물 내용
   document.getElementById("dovView").innerHTML = tmp_msg;
}
doSelectDocView(331);
</script>
 
<div id="dovView"></div><!--// 게시물 내용을 출력할 부분 -->


exec_json()를 이용한 doSelectDocView 스크립트를 이용하여 외부페이지에 게시물 데이타를 불러온 화면

sample2.png





참고로 XE_ROOT/common/xml_handler.js에 선언되어 있는 exec_json 스크립트 내용입니다.


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
67
68
69
70
71
72
73
74
75
76
/**
    * @brief exec_json (exec_xml와 같은 용도)
    **/
    $.exec_json = window.exec_json = function(action, data, callback_sucess, callback_error){
        if(typeof(data) == 'undefined') data = {};
 
        action = action.split('.');
 
        if(action.length == 2) {
            // The cover can be disturbing if it consistently blinks (because ajax call usually takes very short time). So make it invisible for the 1st 0.5 sec and then make it visible.
            var timeoutId = $(".wfsr").data('timeout_id');
 
            if(timeoutId) clearTimeout(timeoutId);
 
            $(".wfsr").css('opacity', 0.0);
            $(".wfsr").data('timeout_id', setTimeout(function(){
                $(".wfsr").css('opacity''');
            }, 1000));
 
            if(show_waiting_message) $(".wfsr").html(waiting_message).show();
 
            $.extend(data,{module:action[0],act:action[1]});
 
            if(typeof(xeVid)!='undefined') $.extend(data,{vid:xeVid});
 
            try {
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: request_uri,
                    contentType: "application/json",
                    data: $.param(data),
                    success: function(data) {
                        $(".wfsr").hide().trigger('cancel_confirm');
                        if(data.error != '0' && data.error > -1000) {
                            if(data.error == -1 && data.message == 'msg_is_not_administrator') {
                                alert('You are not logged in as an administrator');
                                if($.isFunction(callback_error)) callback_error(data);
 
                                return;
                            else {
                                alert(data.message);
                                if($.isFunction(callback_error)) callback_error(data);
 
                                return;
                            }
                        }
 
                        if($.isFunction(callback_sucess)) callback_sucess(data);
                    },
                    error: function(xhr, textStatus) {
                        $(".wfsr").hide();
 
                        var msg = '';
 
                        if (textStatus == 'parsererror') {
                            msg  = 'The result is not valid JSON :\n-------------------------------------\n';
 
                            if(xhr.responseText === ""return;
 
                            msg += xhr.responseText.replace(/<[^>]+>/g, '');
                        else {
                            msg = textStatus;
                        }
 
                        try{
                            console.log(msg);
                        catch(ee){}
                    }
                });
            catch(e) {
                alert(e);
                return;
            }
        }
    };


추천한 사람

 
댓글은 로그인 사용자만 작성 가능합니다. 로그인하기