The code below doesn't change mytoggle from 0 to 1 which is what should happen according to this SO link. Is this somehow Confluence related?
var mytoggle = 0; $.ajax({ type: "PUT", url: "/rest/api/content/" + page_id, data: JSON.stringify(pageData), contentType: "application/json; charset=utf-8", success: function (data) { mytoggle = 1; }, });
alert(mytoggle);
Do NOT use async:false. So many of the comments on that stack overflow post explain why that's a terrible idea. And it's deprecated. That is the WRONG way to handle Ajax.
Just use the .success() callback method and pass 'data' to a function and call alert there.
Thanks, I think it's working. Fyi that SO link presents async: false as the correct way to go, I just joined SO and don't have enough fake internet points to comment. Thanks again.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Agreed, that post is not terribly clear, especially the first answer and it's comments. I have no stack overflow points either, so I also can't comment :/
Good luck!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The stackoverflow post is pretty clear. This is the script, not Confluence.
Ajax is asynchronous. You're calling 'alert()' before the ajax completes. Checkout the stackoverflow post again.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Okay I've tried including async:false like below, which successfully changes mytoggle to 1 but I still can't get my_toggle to only cause a single alert. The code below will give an alert for every successful ajax request instead of just one. Trying to put the if (my_toggle == 1) statement later in the code gives a "my_toggle is not defined" error.
function thisFunction() { for (i = 0; i < inputs.length; i++) { ...
... $.get('/rest/api/content?spaceKey=EXPERTISE&title=' + skillName + '&expand=space,body.storage,version,container', function (data, status) { var pageData = data.results[0]; var existing_body = pageData.body.storage.value; pageData.version.number += 1; var page_id = pageData.id; pageData.body.storage.value = pageData.body.storage.value + "hello world";
var my_toggle = 0; $.ajax({ type: "PUT", async: false, url: "/rest/api/content/" + page_id, data: JSON.stringify(pageData), contentType: "application/json; charset=utf-8", success: function() {
my_toggle = 1;
} });
if (my_toggle == 1){
alert('Success!!'); }); } } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.