2

これは、ユーザーを挿入しようとする場所からの私のjspです

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Add Users using ajax</title>
        <script src="/Spring3MVC/js/jquery.js"></script>
        <script type="text/javascript">
        function doAjaxPost() {
            // get the form values
            var firstName = $('#firstName').val();
            var lastName = $('#lastName').val();

            $.ajax({
                type: "POST",
                url: "/insert",
                data: "firstName=" + firstName + "&lastName=" + lastName,
                success: function(response) {
                    // we have the response
                    $('#info').html(response);
                    $('#firstName').val('');
                    $('#lastName').val('');
                },
                error: function(e) {
                    alert('Error: ' + e);
                }
            });
        }
        </script>
    </head>
    <body>
        <h1>Add Person</h1>
        <table>
            <tr><td>First Name : </td><td> <input type="text" id="firstName" name="firstName" /><br/></td></tr>
            <tr><td>Last Name : </td><td> <input type="text" id="lastName" name="lastName" /> <br/></td></tr>
            <tr><td colspan="2"><input type="button" value="Add Users" onclick="doAjaxPost()"><br/></td></tr>
            <tr><td colspan="2"><div id="info" style="color: green;"></div></td></tr>
        </table>
        <a href="/SpringMVC/search">Show All Users</a>
    </body>
</html>

そして、これは私のコントローラーコードです

@RequestMapping(value="/insert", method = RequestMethod.POST)
public String insertPerson(ModelMap model, HttpServletRequest request, HttpServletResponse response, @RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName ) {
    personDao.savePerson(firstName,lastName);
    model.addAttribute("nameAdded", firstName+" "+lastName);
    return "personAdded";
}

[ユーザーの追加] ボタンをクリックしても何も起こりません。誰でもこれを修正するのを手伝ってもらえますか?

4

4 に答える 4

6

Ajax の投稿以外のあらゆる場所でコンテキスト URL を使用しています。

したほうがいい

url: "/insert",

かもしれない

url: "/SpringMVC/insert",

またはさらに良い

url: "<spring:url value='/insert' />",

于 2012-09-10T07:18:43.067 に答える
4

試す

data: {firstName: firstName, lastName: lastName},

それ以外の

data: "firstName=" + firstName + "&lastName=" + lastName,

あなたのajax呼び出しで。

于 2012-09-10T07:34:45.457 に答える
4

urlJSP for AJAX リクエストを次のように使用します。

url: "insert" 

代わりに:

url: "/insert"

コントローラーではなく、サーバーのルートを指す絶対 URL を使用しています。

私はそれをテストしましたが、あなたのコードでうまく機能しています。

于 2012-09-10T07:38:04.827 に答える
2

what is the absolute url for the two pages, and if you hit f12 key on you keybourd you can trace the ajax call and trace the response code, if it's 200 it's ok nothing wrong with your server side code now check the success function otherwise check the server code and ofcourse 404 means it didn't reach the server.

于 2012-09-10T17:01:05.230 に答える