在继续URL之前检查条件

时间:2017-07-22 08:41:00

标签: jquery json ajax spring-boot thymeleaf

我使用SpringFramework和Thymleaf来设计网页。

我的一个html fiels中有一个链接:

<a th:id="_following_list" th:href="@{'/table'(id=${0})}">Followings</a> 
<span style="color:white;" th:text="${following_count}"></span>

我想转到/ table?id = 0 URL只有在某些条件为真时,否则我想显示警告。此外,当此URL中的函数成功返回时,我想对当前页面中的某些div应用一些更改。

我的想法是使用ajax,类似这样:

$(function () {$("#_follower_list").click(function () {
event.preventDefault();
var $post = $(this);
var toSend = {
    "userId" : 0
}
var currentCredit_followerList = 
parseInt($('#_followerList_credit').text());
if(currentCredit_followerList != 0) {

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: $($post).attr("href"),
        dataType: 'json'
    }).done(function (data) {
        if (data.status == "success") {
            var newCredit_followerList = currentCredit_followerList - 1;
            $('#_followerList_credit').text(newCredit_followerList);
            window.location.href = "table";
            return false;
        }
        else if (data.status == "error") {//do something
        }
        else if (data.status == "ratelimit") {
            window.location.href = "ratelimit";
        }
    });

}else {
    //Show Alarm!
    $('#_credit_error').fadeIn(1000);
}
})
})
问题是链接正在查找其控制器,但我想通过json发送请求。

我可以为这种情况做些什么?我想要:id =&#34; _following_list&#34;是一个链接,所以我不想将其更改为div或删除th:href

0 个答案:

没有答案