Search code examples
javascriptstringget

How to grab a specific part of a returned GET in javascript?


I am trying to return a webpage, and grab the part of the page labeled: Description. I want to be able to split the returned HTML, but with the "" that are in HTML it will not work correctly.

This is for a template to allow a user on an extension to be able to add a pre-loaded string to fill in a box. I have tried splitting the string at a set word, but my knowledge does not extend past trying to use the string.


function pullWebpage(){
var data = null;

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

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    var response = this.responseText;
    response.split("Description");
    console.log(response);
  }
});

xhr.open("GET", "https://jira2.cerner.com/browse/ION-25843?", true);
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data);
}

I want to be able to successfully pull out the description of the page pulled in the GET, and use this to set up an automatic template. HTML returned in GET using postman here


Solution

  • While the issue before was based on grabbing the whole HTML, using the rest api was a better solution. Changing the code to:

    var data = null;
    var xhr = new XMLHttpRequest();
    xhr.withCredentials = true;
    
    xhr.addEventListener("readystatechange", function () {
      if (this.readyState === 4) {
        var response = JSON.parse(this.responseText);
        console.log(response.fields.description);
      }
    });
    xhr.open("GET", "https://jira2.cerner.com/rest/api/2/issue/ION-25843");
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(data);
    

    Allows for the code to go out and grab the json of the page, and then grab the specific definition of the page.