Reputation: 4979
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
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