We have a JSF 2.2 application using PrimeFaces.
Now, when an error occurs, I check for an AJAX request and deliver a partial response (as shown in BalusC's anwer to this question).
But what is, if there's no server anymore to handle the error, e.g. due to connection loss? At the moment, just nothing happens, leaving the user puzzled.
I found a hint in that question, which works, but I'd like to solve this in a general way, so that all AJAX calls which fail try to redirect to the start page - and then may receive the browser connection error message.
For standard JSF ajax, use jsf.ajax.addOnError() to set the default error handler. E.g.
jsf.ajax.addOnError(function(data) {
alert(data.responseText);
});
See also chapter 13.3.6.2 of the JSF 2.2 spec. You can find all properties of data object in table 14-4 of the JSF spec.
For PrimeFaces 4+, hook pfAjaxError event in jQuery (before 4, just use ajaxError). E.g.
$(document).on("pfAjaxError", function(event, xhr, options) {
alert(xhr.responseText);
});
Just customize it accordingly to show some div in top.
Related
I'm using the following code to redirect to another page,
<p:commandButton value="myRedirectButton" value="#{myBean.val}" rendered="#{myBean.renderThis}"
onclick="remoteRedirect();"/>
<p:remoteCommand name="remoteRedirect" actionListener="#{myBean.redirectToPage}"/>
And the actionListener in java is as follows:
public static void redirectToPage(String url){
FacesContext ctx= FacesContext.getCurrentInstance();
ExternalContext ext=ctx.getExternalContext();
String encodedUrl=ext.encodeRedirectUrl(ctx.getApplication().
getViewHandler.getActionUrl(ctx,url),
new HashMap<String,List<String>>());
ext.redirect(encodedUrl);
}
On clicking the 'myRedirectButton', a js error pops up just before the page redirects saying
"This message is only sent, because project stage is development and no other error listeners are registered".
Does anyone have any ideas on why this is happening?
This message is only sent, because project stage is development and no other error listeners are registered
This will show up when you're using MyFaces, and you have javax.faces.PROJECT_STAGE set to Development in web.xml, and you don't have any jsf.ajax.addOnError listener, and the current request is an Ajax request, and the JS code has thrown an error during that request.
Basically, it's just trying to tell you that you should look in the JS console for the actual error, and/or that you should configure a jsf.ajax.addOnError for more fine grained JS error handling. Once having the detail about the actual JS error, you can easily naildown the cause and solve the problem.
As to the concrete problem, you're basically sending multiple ajax requests at the same time on click of the <p:commandButton>, first one via <p:remoteCommand> and immediately thereafter second one via <p:commandButton>. The first one will perform a redirect and hereby unexpectedly abort the second one before its response can be processed, hereby causing a JS error.
There are basically 2 ways to solve your timing problem:
Move the action from the <p:remoteCommand> to the <p:commandButton> and then remove the onclick and <p:remoteCommand> altogether.
Add a return false; in the end of the onclick to block the <p:commandButton> from sending its own ajax request.
I use Ajax extensively in my JavaScript code. Today I added an Ajax call to a page and nothing happens. The Firefox web console shows a result of "undefined". The exact log entry is:
[11:15:50.733] POST http://mastersw.com/theme/test9.php [undefined 78ms]
(I had to modify the URL to satisfy the editor rules here.)
When I click on the log entry, I see a message dialog with no response. Everything else is correct in the message.
I have checked the Apache logs and there is no sign that the post request got to the server. I use my own JavaScript library Ajax routines. They work everywhere else. I have double checked that the script (test9.php) exists.
I cannot find any documentation on what Firefox means when they say "undefined". Google search returns millions of hits about other things.
The problem seems to be that Firefox is for some reason not completing the post operation and I cannot figure out why.
Update: The JavaScript function invoking the Ajax call was itself being invoked from the onclick handler of an anchor. When I changed the element to a div it worked.
I don't have any idea why Firefox gave an 'undefined' for the post. Chrome complained about an invalid header "Content-Length". Changing to a div fixed this as well.
You need to cancel the click event
$("a").on("click", function (evt) {
evt.preventDefault();
//ajax call here
});
I have a question about AJAX.. I am using AJAX for my javascript in calling php file,
how ever I noticed that when I run the program in IE callback comes in twice which
gives me more results than expected while callback comes in in firefox only once..
I want to have just the one reply from callback..
this.doPost = function(param) {
/// make a HTTP POST request to the URL synchronously
req.open("POST",url, true);
...
this is the call..
Do you know what is wrong?
Thanks.
Depending on how you actually perform the AJAX call and what you do in the callback, you can run into issues - see for example this thread for similar issues:
How to execute a page ,that contains JS ,in AJAX ,using innerHTML?
I suggest you try some proven library instead of using AJAX on the low level. For example, using jQuery you can use $.ajax, see e.g. the examples in jQuery documentation:
http://api.jquery.com/jQuery.ajax/
or some of many other examples you can find on the Net, such as this:
http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
I'm creating a webApp in which I show some table data using jQuery and the Flexigrid plugin. In the ideal scenario everything goes ok but I when I return a server-side exception or a validation error on a Flexigrid's ajax request, it does nothing but keep the loading icon and the 'Processing, please wait' message.
Right now you can see this behavior in its sample page, in which the server returns a 404 error but the flexigrid appears like its still loading but never shows any error.
Is there any event I could capture and handle Flexigrid's ajax request errors?
$('#flex').flexigrid({
onError: function(data){
// do stuff here
}
});
Before unobtrusive javascript, I handled ajax complete events with the following register:
Sys.Net.WebRequestManager.add_completedRequest(myHandler);
This event handler will fire every time an ajax request is complete. I also have an ajaxComplete event bind in $(document).ready(), to handle ajax calls exclusively through jQuery:
$.ajaxComplete(function (e, xhr, settings) {
myHandler(xhr)
});
Which also works great. But I get a different behavior when I enabled unobstrusive javascript in ASP.NET MVC 3. It fires the first time when the first ajax call is complete, but on subsequent ajax requests, ajaxComplete event never fires again.
Now, I know about that you need to call $.validate.unobtrusive.parse() to rebuild the validation after the elements in the form updated via partial postback. Is there something similar that I need to do to make sure that ajaxComplete can fire again on subsequent requests? I cannot find the documentation on this.
FYI: I have included all the jquery*.js libs to support unobtrusive javascript. I also have the MicrosoftMvc*.js libs included to support legacy code in the project. I was hoping to convert everything over until I ran into this problem.
You need to actually attach ajaxComplete to an element I thought?
$('.info').ajaxComplete(function() {
var info = $(this);
info.html('Success!');
});
You should only need to attach ajaxComplete to an element once in its lifetime.