Ajax Requst失败 - Java / Json

时间:2015-11-18 01:58:25

标签: javascript java json ajax

我对AJAX很陌生,我觉得我有语法错误,但我无法发现它并且不想浪费另一个半小时寻找它。

我使用Spring安全性登录应用程序,工作正常。成功登录后,我将它们重新路由到' index.jsp。' index.jsp有一个$(document).ready函数,带有一个调用控制器并请求数据的ajax例程。我知道控制器部分有效,它似乎是失败的javascript。

此外,ajax例程中的错误块不会触发。

没有进一步的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());
}

控制器

  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>

0 个答案:

没有答案