I'm pretty new to AJAX and I feel like I have a syntax error but I have not been able to spot it and don't want to waste another hour and half looking for it.
I'm using Spring security to login to the application, which works fine. Upon successfully logging in I re-route them to 'index.jsp.' index.jsp has a $(document).ready function with an ajax routine that calls the controller and requests the data. I know the controller portion works, it's the javascript that appears to be failing.
Also, the error block within the ajax routine does not fire.
Without further ado..
Javascript
var authorTableBody = document.getElementById('authorTableData');
var $body = $('body');
$(document).ready(function () {
if ($body.attr('class') === "splash") {
$.ajax({
type: 'GET',
url: "AuthorController?action=listAjax",
success: function (authors) {
getAllAuthors(authors);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Could not get authors for this user due to: " + errorThrown.toString());
}
});
}
});
function getAllAuthors(authors) {
var index = 0;
var tableParts;
$.each(authors, function () {
index++;
tableParts = ["<tr id='row" + index.toString() + "'" + "class='authorTableDataRow' >",
"<td>", authors.authorId, "</td>",
"<td>", authors.firstName, "</td>",
"<td>", authors.lastName, "</td>",
//"<td>",authors.size,"</td>",
"</tr>"];
});
authorTableBody.append(tableParts.join());
}
Controller
if (action != null) {
try {
String authorID = request.getParameter("authorID");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
Author author = new Author(0);
PrintWriter pw = response.getWriter();
switch (action) {
case "loadTable":
List<Author> authors = authorService.getAllAuthors();
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
authors.forEach((authorObj) ->{
jsonArrayBuilder.add(
Json.createObjectBuilder()
.add("authorId", authorObj.getAuthorID())
.add("firstName", authorObj.getAuthorFirstName())
.add("lastName", authorObj.getAuthorLastName())
//.add("size", authorObj.getBookCollection().size())
);
});
JsonArray authorsJson = jsonArrayBuilder.build();
response.setContentType("application/json");
pw.write(authorsJson.toString());
pw.flush();
return;
HTML (index.jsp)
<div id="authorTableDataContainer">
<form id="authorDeleteForm" action="AuthorController?action=delete" method="POST">
<table id="authorTable" style="text-align: center;">
<thead>
<th> Author ID </th>
<th> First Name </th>
<th> Last Name </th>
<th> Delete </th>
<th View</th>
</thead>
<tbody id="authorTableData">
</tbody>
</table>
</form>
</div>
webconfig.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- Begin Spring-specific configuration -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- END Spring configuration -->
<!-- begin additional Spring Security config -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- end Spring security config -->
<servlet>
<servlet-name>BookController</servlet-name>
<servlet-class>edu.wctc.asp.bookwebapp.controller.BookController</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>BookController</servlet-name>
<url-pattern>/BookController</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>