Confluence API with jquery and json Edited

Hi, 

I'm trying to connect with confluence API. My goal is to create page at choosen space using jqery with json and ajax. Here below is my code:

 

<html>

<head>
<title>Confuelnce API</title>
//headers for jqery
<script type = "text/javascript" src="js/jquery-3.2.1.min.js">
<script type = "text/javascript">
$(document).ready(function(){


//json data to push them to confluence
var data = {
"type": "page",
"title": "New Page 2",
"space": {
"key": "TST2"
},
"body": {
"storage": {
"value": "<p>This is a new page</p>",
"representation": "storage"
}
}
};
//mylink
var link = 'http://localhost:8090/confluence/rest/api/content/';

$.ajax({
url: link,
type: "POST",
dataType: "json",
crossDomain: true,
username: "myusername",
password: "mypass",
data: JSON.stringify(data),
Accept: application/json,
contentType: "application/json",
xhrFields: {
"withCredentials": true
},


success: function (result) {
alert('Data saved!');
},
error : function (xhr, status, error) {
alert(status + ' | ' + error);
}


});
});
</script>
</head>

<body>
<h1>jqery - confluence connection</h1>
</body>

</html>

In browser console (google chrome) I have no errors but in confluence there is no "new page 2" page. Could someone study my code and get a little help to me because I have no idea what I'm doing wrong for now.

Thanks.

1 answer

Hi, 

I am not 100% sure about the code, but I think the line starting with Accept: should be someline like:

accepts: {        text: "application/json"
    }

I am also wondering if you would need to trigger a re-index.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Published 12 hours ago in Confluence

Think you know shares vs. @mentions in Confluence? Take this collab quiz.

To anyone who doubts that Atlassians are a little too obsessed with collaboration, and tools related thereto, let me describe a recent discussion we had (which took place on our internal Confluence, ...

95 views 2 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you