Home > Uncategorized > Asynchronous Ajax in Apex

Asynchronous Ajax in Apex

We use Ajax for rapid query processing without submitting page. In apex, you can build ajax within your pages. For this purposes, we use “Application Process” under the Shared Components.

Sometimes, Application Process can not response rapidly. For example, if you query a count from very large table, it takes for minutes or hours. If you put a text field for table name and if user can query number of records of input big table, also if you perform this operation with ajax in apex as synchronously, the BROWSER will freeze and consumes %100 cpu time. Everything will go bad.

In this situation, you have to use “Asynchronous Ajax” opposite of Synchoronous, so you can more than one job on same time. To perform this, give an eye to this example:

Firstly, you can build your ajax javascript :


function getNumberOfRecords(){
var ajaxRequest = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=GetCount',0);
ajaxRequest.add('P6_TABLE_NAME',html_GetElement('P6_TABLE_NAME').value);
ajaxRequest.GetAsync(f_AsyncReturn);
ajaxRequest = null;
}

This code is used to connect your apex page with your application process. ( By the way, Application Processes are need to created as On Demand ). In normally, synchronous method, you call get() method of ajaxRequest object. Pay attention to this, in asynchronous method, you need to call GetAsync() method by passing an Object. What is the f_AsyncReturn ? :


function f_AsyncReturn(){
if(p.readyState == 1){
$x('P6_TEXTAREA').value = '';
}else if(p.readyState == 2){
}else if(p.readyState == 3){
}else if(p.readyState == 4){
$x('P6_TEXT_AREA').value = p.responseText;
// you can add other things add this area
}else{return false;}
}

If you coded ajax in other languages such as php,jsp; probably you coded as above. There are 4 states to deal with ajax. One of them (state 1) is “Loading”, one of them (state 4) is “Success”. Result of ajax is written in 4. states. Before ajax Request is completed, you can write “Loading” message in first state.

The other operations are the same with Synchronous method. You need to create an Application Process, it’s running time must be “On Demand” as well.

So, by performing this Asynchronous method, your browser will not be freezed. And, not consuming all of CPU time. But disadvantage of this ( may be not seemed apparently ), your job is take much time rather than synchronous method. But, it is worth!

About these ads
Categories: Uncategorized
  1. January 6, 2010 at 9:09 am | #1

    G-D Bless you , solved many needed issues in my commercial apex application.

    Thanks a million

  2. miloSA
    July 16, 2012 at 10:25 pm | #2

    Really, brilliant – I found this very helpfull! Thank you.

    I am running Oracle APEX 4.0 – I wanted to extract XML from an OnDemand process to traverse the DOM and extract values as I needed them, but p.responseText is a purely text object. So, what I did is to use normal JQUERY to traverse it and return values as if you were working with an XMLdoc, in fact you are – here is what I did to take it futher:


    // you can add other things add this area

    //var t = $(‘something’);
    var t = $(p.responseText);

    //loop over ‘bar’ nodes
    t.find(‘bar’).each(function () {
    alert($(this).text());
    });

    For future projects, how do you suggest one return the object as an XML type?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 85 other followers

%d bloggers like this: