Calling an Oracle function via Ajax for on-the-spot validation purposes in Oracle APEX v4.2.2

隐身守侯 提交于 2019-12-01 07:55:56

Ajax + apex 4.2 = apex.server.process api
It requires you have a process at the on-demand process point of the page or an application process. In it you have to call your function and provide the parameters, which can be the page items. To provide a return, write values to the http buffer with calls to htp.p.

DECLARE
  some_var1 VARCHAR2(50);
BEGIN
  some_var1 := my_package.my_function(:P1_EMPNO, :P1_DEPTNO);
  -- write values back
  htp.p(some_var1);
END;

You can easily provide apex.server.process with page items. Further handling is all in javascript.
Note of warning: the dataType is by default set to JSON, and thus if you provide no other default datatype and do not return a json string you will get a parsing error. So if you return a text within your on-demand process such as INVALID, make sure to set the datatype to text!

apex.server.process ( "MY_PROCESS", {
  pageItems: "#P1_DEPTNO,#P1_EMPNO"
  }, {
    dataType: "text"
  , success: function( pData ) { 
      //pData should contain VALID or INVALID - alert it
      alert(pData);
      if ( pData === 'INVALID' ) {
        // do something here when the result is invalid
        // maybe you want to color something red for example
        alert('The data you have entered is invalid');
      };
    }
  } );

I wouldn't split this up in more dynamic actions than necessary, even though it might be possible. I personally am not fond of trying to use a PLSQL block dynamic true action, just because it is more obscure to act on if you want to deal with return values.
Just set your button to not submit the page, but action defined by dynamic action. Then in the dynamic action create one true action of type execute javascript, and use the ajax call with callback(s) there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!