0

When I try to get the request-response with Postman I receive

{score_stacker":25}

Now I want the response saved to a javascript var, trying

  var request = new XMLHttpRequest();
  request.open('GET', 'http://localhost:8000/api/getTenthStacker', true);
  request.send();
  var tenthstacker = JSON.parse(request.responseText);

Now I get a console syntax error

"unexpected end of data at line 1 column 1 of the JSON data", when I use stringify I just have an empty var

What shall I do?

Not A Bot
  • 2,474
  • 2
  • 16
  • 33
summer1337
  • 13
  • 2
  • 3
    You should re-read the doc of XMLHttpRequest, it is not synchrone, you have to use callback to get the response. – Alexandre Nicolas Mar 10 '20 at 09:50
  • 1
    *"What shall I do?"* Use `fetch` instead and learn about promises or `async`/`await`. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch – Felix Kling Mar 10 '20 at 09:53

1 Answers1

0

Your response is not a valid JSON response.

It should be (missing a quote):

{"score_stacker":25}
Tib
  • 2,553
  • 1
  • 27
  • 46