logeeks
logeeks

Reputation: 4979

How to toggle JQuery .$post url

In my program i have written the jquery like below:

$(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") { }
        });
    });
})

What i want to do is to change the $.post url(/project/addhighlight) to the one returned by the backend method once the post is success.

Can any one please advice on how to do it?

Upvotes: 1

Views: 66

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can store the url in a variable which you can update in the callback:

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...
        }
    });
});

Note that jQuery.parseJSON(data) is not needed, as jQuery will automatically parse a JSON response when it detects one.

Upvotes: 1

Related Questions