【问题标题】:Follow/Unfollow button doesn't work when trying to unfollow using same ajax call尝试使用相同的 ajax 调用取消关注时,关注/取消关注按钮不起作用
【发布时间】:2017-04-17 19:45:42
【问题描述】:

场景:页面显示动态生成的按钮(锚点),每个按钮都有一个唯一的 id 和一个公共类。使用类我在 jquery 中检索 id 并将其传递给控制器​​进行处理。虽然我可以单击关注并将其更改为关注,但我无法将其切换回关注。 它给了我“服务器响应状态为 500”。

ajax 代码

            $(document).ready(function() {
            $("body").on(
                    'click',
                    '.follow',
                    function(event) {
                        debugger;
                        event.preventDefault();
                        var cur = "#" + this.id;
                        var toFollow = {
                            "followeeID" : this.id
                        };

                        $.ajax({
                            url : "../follow/add",
                            data : toFollow,
                            dataType : 'json',
                            type : "POST",
                            success : function(data) {
                                if (data) {
                                    console.log(data);
                                    console.log(cur);
                                    $(cur).removeClass("btn-default")
                                            .addClass("btn-success");
                                    $(cur).removeClass("follow").addClass(
                                            "follow");
                                    $(cur).html("Following");
                                } else {
                                    console.log(data);
                                    $(cur).addClass("btn-default")
                                            .removeClass("btn-success");
                                    $(cur).removeClass("follow").addClass(
                                            "follow");
                                    $(cur).html("Follow");
                                }

                            }
                        })

                    });

        });

html代码

 <c:otherwise>
            <table class="table table-stripped table-hover">
                <tr>
                    <th>User Name</th>
                    <th></th>
                </tr>
                <c:forEach items="${requestScope.userlist}" var="user">
                    <c:choose>
                        <c:when test="${user.personID==sessionScope.user.personID}">
                            <tr>
                                <td>${user.username}</td>
                                <td></td>
                            </tr>
                        </c:when>
                        <c:when test="${user.followers.contains(sessionScope.user)}">
                            <tr>
                                <td>${user.username}</td>
                                <td><a class="btn btn-success unfollow" href="#"
                                    id="${user.personID}">Following</a></td>
                            </tr>
                        </c:when>
                        <c:otherwise>
                            <tr>
                                <td>${user.username}</td>
                                <td><a class="btn btn-default follow" href="#"
                                    id="${user.personID}">Follow</a></td>
                            </tr>
                        </c:otherwise>
                    </c:choose>
                </c:forEach>
            </table>
        </c:otherwise>

控制器代码

@RequestMapping(value = "/follow/add", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public boolean addFollower(HttpServletRequest request, @RequestParam String followeeID) throws Exception {
    System.out.println("reached controller");
    boolean flag = false;
    int id = Integer.parseInt(followeeID);
    HttpSession session = request.getSession();
    User follower = (User) session.getAttribute("user");
    try {
        User followee = (User) followerDao.get(id);
        if (followee.getFollowers().contains(follower)) {
            followee.getFollowers().remove(follower);
            System.out.println("removed");
            flag=followerDao.removeFollower(followee);

        } else {
            followee.getFollowers().add(follower);
            System.out.println("added");
            flag=followerDao.addFollower(followee);

        }

    } catch (FollowerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return flag;

} 

【问题讨论】:

  • 我想知道这行代码会做什么? $(cur).removeClass("follow").addClass("follow");删除类并添加相同的?
  • 。当我最初使用两个单独的类和两个单独的控制器函数时,这实际上是添加了一个 .unfollow 类,它们不起作用,所以我没有删除它,然后只是将其更改为跟随。我后来删除了它,但我上传的副本包含
  • 好的,您在删除以下内容时是否在浏览器控制台中收到任何 js 错误?此外,如果您将服务器响应作为内部服务器错误,即 500,那么您删除的后续代码一定有问题,请调试它
  • 我得到的唯一错误是当我第二次点击取消关注时
  • 目前我没有删除 .follow 类,我只是发送到控制器返回一个布尔值,并在此基础上我的按钮发生变化。是的,我尝试了调试器,但第二次它没有进入 $.ajax

标签: java jquery ajax jsp spring-mvc


【解决方案1】:

$(document).ready 函数内的jQuery 代码仅适用于成功加载窗口时可用的DOM 元素,

因此,为了使其适用于动态添加的按钮,您应该使用如下事件绑定:

$(document).on(
    'click',
    '.follow',
    function(event) {
        debugger;
        event.preventDefault();
        var cur = "#" + this.id;
        var toFollow = {
            "followeeID" : this.id
        };

        $.ajax({
            url : "../follow/add",
            data : toFollow,
            dataType : 'json',
            type : "POST",
            success : function(data) {
                if (data) {
                    console.log(data);
                    console.log(cur);
                    $(cur).removeClass("btn-default")
                            .addClass("btn-success");
                    $(cur).removeClass("follow").addClass(
                            "follow");
                    $(cur).html("Following");
                } else {
                    console.log(data);
                    $(cur).addClass("btn-default")
                            .removeClass("btn-success");
                    $(cur).removeClass("follow").addClass(
                            "follow");
                    $(cur).html("Follow");
                }

            }
        })
    });
});

确保在$(document).ready函数之外使用这个jquery绑定代码

【讨论】:

  • 我尝试了上面的代码,但它再次给了我来自服务器的 500 响应
  • 如果它给你服务器响应错误,那么你的服务器端有错误,只有你可以调试。
  • 所以 ajax 调用工作正常吗?因为我无法在 .ajax 函数中进行调试
  • 是的,ajax 工作正常,这就是为什么你得到 500 的响应,在添加动态关注和关注按钮后,上面的答案也将帮助你第二次触发 ajax,依此类推
猜你喜欢
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多