Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Can't access ajax variable outside a request to a confluence page

Daniel Baron April 10, 2018

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

 

2 answers

1 accepted

1 vote
Answer accepted
Steven F Behnke
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2018

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.

Daniel Baron April 12, 2018

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.

Steven F Behnke
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 12, 2018

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! 

Like Deleted user likes this
1 vote
Steven F Behnke
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 10, 2018

The stackoverflow post is pretty clear. This is the script, not Confluence.

  1. You set mytoggle = 0
  2. You start an ajax request
  3. You use an 'alert' to display 'mytoggle'
  4. The ajax request completes and sets mytoggle to 1

Ajax is asynchronous. You're calling 'alert()' before the ajax completes. Checkout the stackoverflow post again.

Daniel Baron April 11, 2018

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!!'); }); } } }

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events