Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Add comment in JIRA through javascript

Leela
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 29, 2018

I am trying to add a comment in JIRA issue using javascript. But its not working. Can someone help me fix this? 

 

<!doctype html>
<html>

<script type="text/javascript">

var usr = 'user1';
var psw = 'pass1';
var sendComment = {"body" : "comment1"};

$("button").on("click",function(){
$.ajax({
type: "POST",
url: "https://xxxxx.atlassian.net/rest/api/2/issue/KEY-123/comment",
dataType: "JSON",
headers: {"Authorization": "Basic " + btoa(usr + ":" + psw)},
data: JSON.stringify(sendComment),
success: function(result) {console.log('success');},
error: function(req, status, err) {console.log('Something went wrong', status, err);}
});
});
</script>

<button>Update JIRA</button>

<body>
</body>
</html>

1 answer

1 accepted

1 vote
Answer accepted
Nir Haimov
Community Champion
July 4, 2018

This will work for you:

var usr = 'user1';
var psw = 'pass1';
var data = JSON.stringify({
"body": "comment test"
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log('success');
}
});

xhr.open("POST", "https://xxxxx.atlassian.net/rest/api/2/issue/KEY-123/comment");
xhr.setRequestHeader("authorization", "Basic " + btoa(usr + ":" + psw));
xhr.setRequestHeader("content-type", "application/json");
xhr.send(data);
Vincent Clerc
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 25, 2019

Great stuff! Thanks!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events