weixin_33714884 2015-11-18 01:58 采纳率: 0%
浏览 9

Ajax请求失败-Java / Json

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>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)