Javascript 如何切换JQuery.$post url

Javascript 如何切换JQuery.$post url,javascript,jquery,Javascript,Jquery,在我的程序中,我编写了如下jquery: $(document).ready(function() { $("#toggle").click(function() { $.post("/project/addhighlight", { name: "Donald Duck", city: "Duckburg" }, function(data, status){ var

在我的程序中,我编写了如下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
)更改为后端方法返回的url


任何人都可以就如何操作提供建议吗?

您可以将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(数据)
,因为当jQuery检测到JSON响应时,它将自动解析JSON响应。

请确保拼写成功,区分大小写,并且可能重复该响应