APEX中使用AJAX及其调试方法

1.APEX中AJAX的原理

此处的AJAX,其原理与其他语言中使用的一样,We can define it for each region in the Region Source area, or in the HTML Header area of a page’s attributes.如下所示[参考资料:1]:

var ajaxRequest = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=getEmployees',0);
ajaxRequest.add('P1_EMPLOYEE_NARROW',narrowText.value);
ajaxResult = ajaxRequest.get();

2. 使用步骤

  • 在页面上创建一个Page Item Button,将其HTML Table Cell Attributes属性设置为onClick="saveResult()";
  • 在页面的HTML Header and Body Attribute的HTML Header输入框里面填写javascript/ajax脚本(此处可以使用javascript api:http://docs.oracle.com/cd/E23903_01/doc/doc.41/e21676/javascript_api.htm)
  • // JavaScript Document
    
  • 在Shared Components里面创建一个application process SAVE_REPORT,里面引用了从ajax传递过来的参数P1_VALLUE2SAVE的值
  • DECLARE
      Id number(10);
    BEGIN
      Id:=TRUNC(DBMS_RANDOM.VALUE(0,10000));
      INSERT INTO RESULTS (JOB_ID,TEST) VALUES (Id,:P1_VALLUE2SAVE);
      COMMIT;
    END;
    
  • 运行页面,点击button,就可以看到数据插入到了RESULTS表格了。

3.实例[参考资料:2]

http://apex.oracle.com/pls/otn/f?p=31517:80:2359116662402201::NO



4. 调试

上述例子,在APEX 4.1中运行时为空白页,而APEX没有提示任何错误信息,这时候,我们可以使用下面的url测试Application Process是否正常调用了,结果出现Process Not Found的错误,后来将Application Process名称修改为大写的GET_DETIAL_PROCESS运行成功。

测试url:

http://hostname:8888/apex/f?p=131:16:269493785121911:APPLICATION_PROCESS=GET_DETIAL_PROCESS:::P16_NAME:CLARK


So what to do if APEX doesn’t tell us anything?[参考资料:3]

You first have to figure out where the problem of your On-Demand Process is.

  • Has a wrong process name be specified?
  • Does it raise an exception which causes a blank page?
  • Does the PL/SQL code not compile?

The best way to test it is to start the On-Demand Process directly through the proper URL. It’s much easier than calling it from some JavaScript code where you don’t see what is returned.

Use the following syntax to call it:

f?p=application_id:0:session:APPLICATION_PROCESS=process_name:::item-names:item-values

Where:

  • application_id is the application ID or alphanumeric alias
  • session is the session ID (run your application and get it from the URL)
  • APPLICATION_PROCESS=process_name is the keyword APPLICATION_PROCESS= followed by either the process ID or an alphanumeric name of an application-level process having a Process Point of On Demand
  • item-names are a list of application- or page item names which you want to set. They are seperated by a comma
  • item-values are a list of values corresponding to the item-names, also separated by a comma.

An example URL would be

http://apex.oracle.com/pls/otn/f?p=33231:0:1725326667635628:

APPLICATION_PROCESS=ApexLib_getLovResult:::APEXLIB_REFERENCE_TYPE,APEXLIB_REFERENCE_ID:ABC,123

The best way to get the correct process name is to copy-paste it from the process definition. It wouldn’t be the first time that there is a type. 

Didn’t work? The next step is to replace the existing PL/SQL code of the On-Demand Process with just the following code, to make sure that the call works at least.

HTP.p('Hallo world');

Didn’t work, too? Then I have no clue neither what to do next in such a case. Check again the URL and process name if they are ok.

If you got the “Hallo world” then it seems that your PL/SQL code is raising an exception or thePL/SQL code is syntactically wrong.

To handle the first possibility add an exception handler around your code, to show us the exception which is raised by Oracle.

BEGIN
  [...Here comes your existing code...]
EXCEPTION WHEN OTHERS THEN
  HTP.p('Error: '||SQLERRM);
END;

Does it show an error message? With this error message you should have a hint what’s going wrong in your code. If not, add some HTP.P calls for debugging purpose to your code.

If it still doesn’t show an error message, than it seems that your PL/SQL code is syntactically wrong. Have you already tried to run/compile the code stand alone in SQL*Plus/Toad/SQL Developer?

Happy testing!


【参考文献】

[1]http://www.dba-oracle.com/t_html_db_apex_ajax_application_express.htm

[2]http://apex.oracle.com/pls/otn/f?p=31517:80:2359116662402201::NO

[3]http://www.inside-oracle-apex.com/how-to-test-an-on-demand-process-used-for-ajax/

[4]http://www.packtpub.com/article/ajax-implementation-apex



你可能感兴趣的:(apex)