1

function output(inp) {
      document.body.appendChild(document.createElement('pre')).innerHTML = inp;
  }


  function searchInGitRepo(str) {
    const http = new XMLHttpRequest();
    http.open("GET", "https://api.github.com/search/repositories?q=" + str);
    http.send();
    http.onload = () => {
      var result = http.responseText;
      var strformat = JSON.stringify(result, undefined, 4);
      output(strformat);
    }
  }

  var result = searchInGitRepo('ecommerce');

I want to get data from github reponsitories. Result is coming perfectly but data is not formatting. Please take a look my code. It should work. I have followed many solution. Nothing is working for me!

function searchInGitRepo(str) {
    const http = new XMLHttpRequest();
    http.open("GET", "https://api.github.com/search/repositories?q=" + str);
    http.send();
    http.onload = () => {
      var result = http.responseText;
      var strformat = JSON.stringify(result, undefined, 4);
      output(strformat);
    }
}

Output Function:

function output(inp) {
      document.body.appendChild(document.createElement('pre')).innerHTML = inp;
}

1 Answer 1

1

JSON.stringify() is used to convert objects into strings. It's usually used when sending data to a web server because those data has to be a string.

In your case, the data result that you are getting back from the server are already string, thus you don't need to "stringify" it.

Use output(result); instead of output(strformat);

Sign up to request clarification or add additional context in comments.

3 Comments

Great. Thank you.
I added an explanation for you if you are interested
Got it. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.