Popovici Sebi
Popovici Sebi

Reputation: 258

I don't get any response from JIRA api called from javaScript

I want to get from jira a list of all tickets , I used XMLHttpRequest(), but I get an error when I try to parse the response with json (unexpected end of data at line 1 column 1 of the JSON data), this is my code:

<script type="text/javascript">
function request(){
  var xhr = new XMLHttpRequest();
  baseURL="...jira/rest/api/2/...";
  xhr.open("GET", baseURL, true);
  xhr.setRequestHeader("Authorization", "Basic "+btoa("userName:password"));
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.send();
  var response = JSON.parse(xhr.responseText);
  document.write(response);
}
request();
</script>

Upvotes: 1

Views: 268

Answers (1)

Quentin
Quentin

Reputation: 943214

xhr.responseText will be undefined at that point.

You have to wait for the response before trying to read the responseText.

xhr.send();
xhr.addEventListener("load", function () {
    var response = JSON.parse(xhr.responseText);
    document.write(response);
});

Upvotes: 2

Related Questions