I am using Oracle 11g with Oracle Apex v4.2.2 and I was wondering how the best way to call an Oracle function via an ajax call, within a Dynamic Action.

I basically have a function that takes six parameters that either returns the result of 'INVALID' or 'VALID'.

Within my page, I want to be able to accept the values that the user has entered and once they press the button to process, I need to check via ajax whether the result was 'INVALID' or 'VALID' and immediately present the user with a dialog box notifying them that there was an error.

Is there a new means of processing this type of ajax request to call a function, within Oracle APEX v4.2.2?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top