javascript 在 Oracle APEX v4.2.2 中通过 Ajax 调用 Oracle 函数以进行现场验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21474303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:59:27  来源:igfitidea点击:

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

javascriptajaxoracle11goracle-apex

提问by tonyf

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.

我正在将 Oracle 11g 与 Oracle Apex v4.2.2 一起使用,我想知道如何在动态操作中通过 ajax 调用调用 Oracle 函数的最佳方法。

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.

在我的页面中,我希望能够接受用户输入的值,一旦他们按下按钮进行处理,我需要通过 ajax 检查结果是“无效”还是“有效”并立即向用户显示一个对话框,通知他们有错误。

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

在 Oracle APEX v4.2.2 中,是否有一种新方法可以处理此类调用函数的 ajax 请求?

回答by Tom

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.

Ajax + apex 4.2 = apex.server.process api
需要你在页面的按需处理点或者应用进程有一个进程。在其中您必须调用您的函数并提供参数,这些参数可以是页面项目。要提供返回值,请通过调用 将值写入 http 缓冲区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.processwith 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页面项目。进一步处理全部在 javascript 中。
警告说明:dataType 默认设置为 JSON,因此如果您不提供其他默认数据类型并且不返回 json 字符串,您将收到解析错误。因此,如果您在按需流程中返回文本(例如 INVALID),请确保将数据类型设置为文本!

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.

即使有可能,我也不会将其拆分为不必要的动态操作。我个人不喜欢尝试使用 PLSQL 块动态真实操作,只是因为如果您想处理返回值,它会更加模糊。
只需将您的按钮设置为不提交页面,而是由动态动作定义的动作。然后在动态操作中创建一个真正的执行 javascript 类型的操作,并在那里使用带有回调的 ajax 调用。