Ajax Callback: 呼叫DB Funcction取得值
Process_name=set_clock_in
declare
v_clock_in varchar2(20);
begin
SELECT get_clock_in_fun( to_char(sysdate,'yyyy-mm-dd'))
into v_clock_in
from dual;
htp.p(v_clock_in);
end;
Javascript: AJAX Call API
apex.server.process("set_clock_in" //Process or AJAX Callback name
, null //Parameter
, {
success: function(pData){
apex.item("P1_CLOCK_IN").setValue(pData);
//alert('Record loaded successfully');
},
dataType: "text"
}
);
PLSQL Call API Function:
get_clock_in_fun
CREATE OR REPLACE function get_clock_in_fun( v_date IN varchar2)
return varchar2
is
-- Local variables here
i integer;
l_resp clob;
g_url varchar2(300) := 'https://example-api.company.com/clock-in/v_date;
v_hashkey varchar2(128) := '.........................................................................';
v_hashdate varchar2(20);
v_token varchar2(200);
v_resp varchar2(4000);
web_failure_detected EXCEPTION;
PRAGMA EXCEPTION_INIT(web_failure_detected, -29273);
begin
--Authorization : {Hash Key}:{UTC datetime}
select to_char(systimestamp at time zone 'UTC','yyyymmddhh24miss')
into v_hashdate
from dual;
select utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(v_hashkey||':'||v_hashdate)))
into v_token
from dual;
--utl_encode.base64_encode()編碼生成的字串以64個字元為一組其後添加回車換行字元
--利用正則式替換掉所有空白字元,不管是否為回車換行字元
v_token:=regexp_replace(v_token, '\s', '');
apex_web_service.g_request_headers(1).name := 'Authorization';
apex_web_service.g_request_headers(1).value := 'Bearer '||v_token;
-- Content-Type
apex_web_service.g_request_headers(2).name := 'Content-Type';
apex_web_service.g_request_headers(2).Value := 'text/html';
--HTTP 411 header加入Content-Length
apex_web_service.g_request_headers(3).name := 'Content-Length';
apex_web_service.g_request_headers(3).value := 0;
l_resp := apex_web_service.make_rest_request(p_url => g_url,
p_http_method => 'GET'
--p_parm_name => apex_util.string_to_table('',':'),
--p_parm_value => apex_util.string_to_table('',':')
--p_wallet_path =>null,
--p_wallet_pwd => null
);
v_resp := substr(l_resp, 1, 4000);
with date_array (col) as
(select v_resp
from dual)
SELECT min(val) INTO v_resp FROM
(
select regexp_substr(replace(substr(col,instr(col,'[')+1, instr(col,']')-2), '"', ''), '[^,]+', 1, level) val
from date_array
connect by level <= regexp_count(col, ',') + 1
);
return v_resp;
EXCEPTION
WHEN web_failure_detected THEN
DBMS_OUTPUT.put_line ('web failure detected');
RETURN ':';
WHEN OTHERS THEN
RETURN '--';
END;
沒有留言:
張貼留言