【问题标题】:How to toggle JQuery .$post url如何切换 JQuery .$post url
【发布时间】:2014-07-23 06:44:43
【问题描述】:

在我的程序中,我编写了如下的 jquery:

$(document).ready(function() {
    $("#toggle").click(function() {
        $.post("/project/addhighlight", {
            name: "Donald Duck",
            city: "Duckburg"
        },
        function(data, status){
            var dataconverted = jQuery.parseJSON(data);
            $("#mytext").text(dataconverted);
            if (status == "success") { }
        });
    });
})

我想要做的是将$.post url(/project/addhighlight) 更改为发布成功后后端方法返回的那个。

谁能给点建议?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

您可以将 url 存储在一个可以在回调中更新的变量中:

var postUrl = '/project/addhighlight';
$("#toggle").click(function() {
    $.post(postUrl, {
        name: "Donald Duck",
        city: "Duckburg"
    },
    function(data, status) {
        $("#mytext").text(data);
        if (status == "success") {
            postUrl = data.updatedPostUrl; // your property here...
        }
    });
});

请注意,jQuery.parseJSON(data) 不是必需的,因为 jQuery 会在检测到 JSON 响应时自动解析。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-02
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2015-04-07
    • 2010-10-19
    • 1970-01-01
    相关资源
    最近更新 更多