Surfaces: standard default method for all ajax requests

I am trying to configure the oncomplete method for all ajax requests so that I can handle the session timeout.

I tried to add the following script, but it did not work the same as setting the oncomplete property for the p: ajax element. It will not be executed every time an Ajax request is executed.

$.ajaxSetup({method: post, complete: function(xhr, status, args){ var xdoc = xhr.responseXML; if(xdoc == null){ return; } errorNodes = xdoc.getElementsByTagName('error-name'); if (errorNodes.length == 0) { return; } errorName = errorNodes[0].childNodes[0].nodeValue; errorValueNode = xmlDoc.getElementsByTagName('error-message'); errorValue = errorValueNode[0].childNodes[0].nodeValue; alert(errorValue); document.location.href='${pageContext.request.contextPath}/login/login.jsf'; } }); 

Any help would be appreciated

+4
source share
3 answers

I managed to implement this by wrapping the Primefaces AjaxUtils method.

 var originalPrimeFacesAjaxUtilsSend = PrimeFaces.ajax.AjaxUtils.send; PrimeFaces.ajax.AjaxUtils.send = function(cfg) { if (!cfg.oncomplete) { // register default handler cfg.oncomplete = oncompleteDefaultHandler; } originalPrimeFacesAjaxUtilsSend.apply(this, arguments); }; 
+5
source

New versions of PrimeFaces (PF 5 is used here)

 var originalPrimeFacesAjaxUtilsSend = PrimeFaces.ajax.Request.send; PrimeFaces.ajax.Request.send = function(cfg) { if (!cfg.oncomplete) { cfg.oncomplete = doYourStuff; } originalPrimeFacesAjaxUtilsSend.apply(this, arguments); }; 

Just to save it somewhere, I tried to find it on stackoverflow, but only older versions .. hope someone finds this useful.

+6
source

In primary, there is ajaxStatus component that you can use for this purpose. Read the documentation to see more details about this, but for your use case it might be something like this:

 <p:ajaxStatus oncomplete="ajaxStatusHandler(xhr, status, args)"/> 

and you can use your JavaScript function as is:

 function ajaxStatusHandler(xhr, status, args) { // your code ... } 

NOTE. This method can only be used for global AJAX requests (which is used by default in PrimeFaces), as well as I know, a cross-domain script or JSONP (padded JSON) cannot be global.

+2
source

All Articles