【问题标题】:XHR failed loading: POST java springXHR 加载失败:POST java spring
【发布时间】:2017-12-04 10:46:52
【问题描述】:

我正在尝试使用 ajax 将数据从我的 jsp 页面发送到我的控制器。 但是我无法完成它,每次我收到帖子标题中显示的错误。

JSP 内容:(这是我加载所有用户的模式,我想通过将我的用户 ID 和我当前的项目 ID 发送到我的控制器来将它们添加到项目中)

<div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"></h4>
                </div>
                <div class="modal-body">
                    <h1 class="text-center">UserList</h1>
                    <br><br>
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Photo</th>
                                <th>Firstname</th>
                                <th>Lastname</th>
                                <th>Function</th>
                                <th>Email</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:forEach var="u" items="${users}">
                                <tr>
                                    <td><img src="${u.getPhoto()}"
                                             alt="Alternate Text" class="img-responsive" /></td>
                                    <td>${u.getFirstname()}</td>
                                    <td>${u.getLastname()}</td>
                                    <td>${u.getFunction()}</td>
                                    <td>${u.getEmail()}</td>
                                    <td><Button type="Button" onclick="myFunction()" id="addButton" class="btn btn-default">Add</button></td>
                            <input type="hidden" id="currentProjectId" value="${p.getProjectId()}">
                            <input type="hidden" id="userId" value="${u.getUserId()}">
                            </tr> 
                        </c:forEach>
                        </tbody>
                    </table>

                    <script>
                        function myFunction()
                        {
                            var currentProjectId = $('#currentProjectId').val();
                            var userId = $('#userId').val();

                            $.ajax({
                                type: "POST",
                                contentType: "application/json",
                                url: "addUserToProject",
                                data: JSON.stringify({projectId: currentProjectId, userId: userId}),
                                dataType: 'json',
                                timeout: 600000,
                                success: function (data) {
                                    console.log("SUCCESS: ", data);
                                },
                                error: function (e) {
                                    console.log("ERROR: ", e);
                                }
                            });
                        }
                    </script>

                </div>
            </div>
        </div>
    </div>

控制器内容:

@RequestMapping(value = "/addUserToProject", method = RequestMethod.POST)
public @ResponseBody
String addUser(@RequestParam("projectId") String projectId, @RequestParam("userId") String userId) {

    UUID uid = UUID.fromString(userId);
    UUID pid = UUID.fromString(projectId);
    User u = UserList.getInstance().readUserById(uid);
    Project p = ProjectList.getInstance().readProjectByID(pid);

    ProjectList.getInstance().addUsertoProject(u, p);
    return "redirect:/projectpage";
}

我现在得到的结果是这样的: Console output

我在这里做错了什么?任何帮助将不胜感激!

【问题讨论】:

  • $.ajax.data 中,您设置的是 POST body,但在 @RequestParam 中,您期望它在 POST parameter 中。
  • 谢谢!你能告诉我我应该怎么做吗?

标签: javascript java jquery ajax spring


【解决方案1】:

您应该将参数传递给您要发布的url,例如:

url: "/addUserToProject?projectId=" + currentProjectId + "&userId=" + userId;

不要忘记从您的 ajax 帖子中删除“数据”部分。

PS:如果你想在你的ajax请求中使用'data',你需要从你的Java方法中移除@RequestParam注解。相反,您可以创建一个新的模型类并将参数及其 getter 和 setter 方法放在一起。使用@RequestBody 注释将该模型传递给Java 方法。然后你可以在你的ajax请求中使用“数据”。

【讨论】:

  • 感谢您的回复!我知道我可以在 url 中传递参数,但我不想打开新页面。
  • 当我尝试 @RequestBody 时,它给了我“不支持的媒体”的 415 错误
猜你喜欢
  • 2016-11-17
  • 2017-03-09
  • 2021-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 2018-08-02
  • 2020-12-21
相关资源
最近更新 更多