Jquery默认执行Error部分 - Servlet

时间:2018-02-13 14:58:56

标签: javascript java jquery jsp servlets

我是Ajax和JQuery的新手。我试图在不提交表单的情况下将数据插入到我的数据库中。数据将插入表中,但是:

  1. 我无法在我的jsp上获得错误消息或成功消息。它来自[object Object]
  2. 看起来Jquery的成功部分根本没有执行
  3. 我的Jquery:

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
            <script>
                $(document).ready(function () {
                    //On Button Click
                    $("#countrybutton").click(function () {
                        //Get Text box values
    
                        var country = $("#country").val();
                        var continent = $("#continent").val();
                        var region = $("#region").val();
                        var population = $("#population").val();
                        var capital = $("#capital").val();
                        $.ajax({
                            url: 'do?MOD=BOK&ACT=domcountry&country=' + country + "&continent=" + continent + "&region=" + region + "&population=" + population + "&capital=" + capital,
                            dataType: "json",
                            type: "Post",
                            success: function (result) {
                               $("#message").text(result);
                                //Clear Textbox data
                                $("#country").val('');
                                $("#continent").val('');
                                $("#region").val('');
                                $("#population").val('');
                                $("#capital").val('');
                            },
                            error: function (responseText) {
                                $("#message").text(responseText);
                            }
                        });
                    });
                });
            </script> 
    

    我的JSP

      <table width="100%" border="0" align="left" cellpadding="5" cellspacing="1">
                    <th colspan="5" align="left" scope="col"></th>
                    <tr>
                        <td colspan="2" id="message" style="color: red; font-size: 14px;"></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>Country Name</td>
                        <td style="color: red">*</td>
                        <td><label>
                                <input name="country" value="${country}" onkeyup="this.value = this.value.toUpperCase();"  type="text" id="country"/>
                            </label></td>
                        <td>&nbsp;</td>
                        <td>Continent Name</td>
                        <td style="color: red">*</td>
                        <td><label>
                                <input name="continent" value="${continent}" onkeyup="this.value = this.value.toUpperCase();"   type="text"  id="continent"/>
                            </label></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>Region</td>
                        <td style="color: red">*</td>
                        <td><label>
                                <input name="region" value="${region}"  onkeyup="this.value = this.value.toUpperCase();"  type="text" id="region"/>
                            </label></td>
                        <td>&nbsp;</td>
                        <td>Population</td>
                        <td style="color: red">*</td>
                        <td><label>
                                <input name="population" value="${population}"  type="text"  id="population"/>
                            </label></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>Capital</td>
                        <td style="color: red">*</td>
                        <td><label>
                                <input name="capital" value="${capital}"   type="text" id="capital"/>
                            </label></td>
                        <td>&nbsp;</td>
    
                    </tr>
                </table>
            </div>
            <div>
                <table width="100%" border="0" align="left" cellpadding="5" cellspacing="1">
                    <th colspan="5" align="left" scope="col"></th>
                    <tr>
                        <td>&nbsp;</td>
                        <td><div align="right">
                                <input type="reset" name="Submit2" value="Reset" class="redButton" />
                            </div></td>
                        <td><label>
                                <input name="Submit" class="redButton" type="button" id="countrybutton" onclick="MM_validateForm('country', '', 'R', 'continent', '', 'R', 'region', '', 'R', 'population', '', 'R',
                                                'capital', '', 'R');
                                        return document.MM_returnValue" value="Submit" />
                                <td>&nbsp;</td>
                    </tr>
                </table>
    

    我的servlet部分:

    response.setContentType("application/json");
                            String country = request.getParameter("country");
                            String continent = request.getParameter("continent");
                            String region = request.getParameter("region");
                            int population = Integer.parseInt(request.getParameter("population"));
                            String capital = request.getParameter("capital");
                            String returnMessage;
                            if (Countries.addCountry(country, continent, region, population, capital)) {
                                returnMessage = "Record Inserted.";
                            } else {
                                returnMessage = "Unable to Insert Record.";
                                try {
                                    throw new InsertException("Unable to Insert record");
                                } catch (InsertException ex) {
                                    log.debug(ex.getMessage());
                                }
                            }
                            new Gson().toJson(returnMessage, response.getWriter());
                            response.sendRedirect("index.jsp");
    

    我错过了什么?

1 个答案:

答案 0 :(得分:1)

尝试删除此行:

response.sendRedirect("index.jsp");

并且为了在Ajax错误中获得异常,您需要设置http代码500作为响应,将其添加到“else”中:

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

Servlet决赛:

response.setContentType("application/json");
String country = request.getParameter("country");
String continent = request.getParameter("continent");
String region = request.getParameter("region");
int population = Integer.parseInt(request.getParameter("population"));
String capital = request.getParameter("capital");
String returnMessage;
if (Countries.addCountry(country, continent, region, population, capital)) {
    returnMessage = "Record Inserted.";
} else {
    returnMessage = "Unable to Insert Record.";
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    try {
        throw new InsertException("Unable to Insert record");
    } catch (InsertException ex) {
        log.debug(ex.getMessage());
    }
}
new Gson().toJson(returnMessage, response.getWriter());