Asynchronous Ajax with Apex

While doing a short project with Oracle Apex we needed an asynchronous ajax call where we changed the page based on the result. Almost all the examples we could find did nothing with the response of the asynchronous call which made it take some time to figure out how to make it work.

The first stage of the asynchrounous call is exactly the same as synchronous calls in Apex:

function searchAsynchronous() {
	var get = new htmldb_Get(null,html_GetElement('pFlowId').value,
		'APPLICATION_PROCESS=AP_SEARCH',0);
	get.add('AI_SEARCH', html_GetElement("AI_SEARCH").value);
	get.GetAsync(asyncReturn);
	get = null;
}

Whenever the state of the asynchronous call changes the f_AsyncReturn function gets called. When the response is available the state will be 4.

function asyncReturn(){
	if(p.readyState == 1){
	}else if(p.readyState == 2){
	}else if(p.readyState == 3){
	}else if(p.readyState == 4){
		handleResult(p.responseXML);
	}else{return false;}
}

In our case we needed the XML DOM model of the response so we used the responseXML attribute as an argument to the function that handles the response of the search.

function handleResult(responseXML) {
	if(responseXML) {
		var count = responseXML.getElementsByTagName("row").length;
		if (count > 0) {
			for(var i=0; i &lt ; count;i++) {
				var item = responseXML.getElementsByTagName("row")[i];
				var content = item.firstChild.nodeValue;
				alert(content);
			}
		} else if (count == 0) {
			alert("no result");
		}
	}
}

The responseXML is comparable with the object that is returned when get.get('XML') is used instead of get.GetAsync(asyncReturn). If the ajax request returns plain text, or should be parsed otherwise p.responseText can be used to give a similar result to get.get()

3 thoughts on “Asynchronous Ajax with Apex

  1. Hi Thomas,
    I have been looking for this explanation for over an hour. I’m not even sure where to start looking in the Oracle documentation, but this explanation was concise and exactly what I was looking for.

    Thank You!

    -David

Leave a Reply

Your email address will not be published. Required fields are marked *