Tuesday, October 10, 2017

Ajax not working in JSF 2 application

Leave a Comment

I'm getting errors when trying to refresh the form with <f:ajax render="@form" listener="#{myBean.onSelectItemChange}" />

The message in the popup is "emptyResponse: An empty response was received from the server. Check server error logs."

In Firefox console, the error is "XML Parsing Error: XML or text declaration not at start of entity Location"

The same code works well in my local environment. I use Tomcat 9 in both local and production environments. I'm getting errors only in production.

The xhtml page starts with

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> 

When rendered, the <!DOCTYPE html>is added automatically at the top of the page.

I've also tried adding <?xml version="1.0" encoding="UTF-8"?> on the top of the page.

Edit: To be more specific on error: XML Parsing Error: XML or text declaration not at start of entity Location: http://serverIP:8080/app/myPage Line Number 2, Column 1:

<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html> 

UPDATE: The XHR response looks like this:

<?xml version='1.0' encoding='UTF-8'?> <?xml version='1.0' encoding='UTF-8'?> <partial-response> ... </partial-response> 

The first line is repeated twice

By the way, the app is working correctly on my development environment. I'm using Tomcat9 on both development(Windows) and production (Ubuntu) computers.

2 Answers

Answers 1

If the client logs this error, the server side XHTML is not to blame or only indirectly. Looking what the real response is in the browswer developer tools will show the real cause.

After you did this it became obvious that there was a 'duplicate' prolog. Using this in a title (or in a search in google) would have most likely pointed to an existing Q/A in in Stackoverflow and effectively making this Q/A a duplicate of it: Ajax update has no effect, Firefox errors: XML or text declaration not at start of entity

I still post this as an answer for two reasons:

  • To show others the relevance of 'root cause analysis'
  • It cannot be marked as a duplicate because of the bounty on it

Answers 2

Try to locate the absolute id of your form in the browser using "Inspect elements" then change the render value to this id
you will have something like this :

<f:ajax render="element:element1" listener="#{myBean.onSelectItemChange}"></f:ajax> 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment