-1

I have tried different code and settings, but i still not working how it should be. I would like to add all the values from the array in to the makeSpaceInactive function of jQuery..

$(document).ready(function() {

var myArray = [ 'AB', 'AC']

function makeSpaceInactive(spaceKey) {
    jQuery.ajax({
        contentType: 'application/json',
        type: 'POST',
        url: '/rpc/json-rpc/confluenceservice-v2/setSpaceStatus',
        data: '["SPACEKEY", "ARCHIVED"]',
        success: function(response) {
          console.log(response);
        }, error: function(response) {
          console.log(response);
        }
    }); 
});

$.each(myArray, function (index, value) {
    makeSpaceInactive(value);
});

})

The values in the myArray should be looped in the makeSpaceInactive function with the data function where the SPACEKEY is. But i don't know how to replace the SPACEKEY data with the myArray values?

Danny
  • 189
  • 2
  • 15

3 Answers3

0

Well you've got some sintax errors, like the last }), but that's not what I've understood from your question. From what I've understood your problem is knowing how to pass the POST parameters to the AJAX request.

Simply replace the ['SPACEKEY','ARCHIVED'] with {arrayelement:spaceKey} and if you want more elements to be passed separate them by a comma. E.g. {arrayoneelement:spaceKey1,arraytwoelement:spaceKey2}

Please refer to this fiddle. (I've left the AJAX request commented but with the data values correctly filled).

Hope this helps you. If you need more advice let me know.

EDIT: Fiddle link was wrong, been corrected, sorry.

Community
  • 1
  • 1
H. Figueiredo
  • 888
  • 9
  • 18
  • Thank you. I have tried it but i'm getting the following error with the script you have created in Fiddle: "The request body could not be parsed as JSON." – Danny May 24 '17 at 12:30
  • @DannyvandenBerg I suppose that error occurs in the AJAX call, remember that you've set `contentType: 'application/json',` and that means that whatever is the response from `/rpc/json-rpc/confluenceservice-v2/setSpaceStatus` needs to be JSON. I think that error comes from "setSpaceStatus" you are requesting. Try setting contentType to 'text/plain'. – H. Figueiredo May 24 '17 at 13:28
0

As you said in your comment, you want to pass one value at a time, so that several AJAX requests would be made. The problem is that the "A" in "AJAX" stands for "asynchronous". So you can't assure that your program will wait the response before sending a new request.

You might want to change your solution, I don't think it's a good idea to make several AJAX requests for every element inside the array. Try sending your array as data and make the server-side program interpret it. Something like:

function makeSpaceInactive(myArray) {
    jQuery.ajax({
        contentType: 'application/json',
        type: 'POST',
        url: '/rpc/json-rpc/confluenceservice-v2/setSpaceStatus',
        data: {arrayData:myArray,status:'ARCHIVED'},
        success: function(response) {
          console.log(response);
        }, error: function(response) {
          console.log(response);
        }
    }); 
});

And let your served-side script loop the array and return an appropriate response. Hope it helps.

rbock
  • 625
  • 5
  • 15
  • Thank you! It's almost working i guess. But now i'm getting the following error: RPC method not found: setSpaceStatus taking 1 parameters – Danny May 29 '17 at 08:20
0

I got it finally working. Thank you for your support!

var myArray ['spaceKey', 'spaceKey'];

function makeSpaceInactive(value) {
    jQuery.ajax({
        contentType: 'application/json',
        type: 'POST',
        url: '/rpc/json-rpc/confluenceservice-v2/setSpaceStatus',
        data: JSON.stringify([value, "ARCHIVED"]),
        success: function(response) {
          console.log(response);
        }, error: function(response) {
          console.log(response);
        }
    });
};

$.each(myArray, function (index, value) {
  makeSpaceInactive(value);
});
Danny
  • 189
  • 2
  • 15