JSON.parse Unexpected end of JSON input but the code process correctly the data

Issue

This Content is from Stack Overflow. Question asked by NicoCaldo

I’m using a Cloudflare worker to return a JSON. The code running on the worker is pretty simple

//return JSON
    const data = {
      pswd: psk_db,
    };

    json = JSON.stringify(data, null, 2);
  }

  return new Response(json, {
      headers: {
        'content-type': 'application/json;charset=UTF-8',
        "Access-Control-Allow-Origin": "*",
      },
    })

Now, for some reason, I correctly recieve the response but when I call the javascript

var parsed = JSON.parse(Http.response);
        document.getElementById("json_response_code").textContent = parsed.pswd;

I got

Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at RequestCode.Http.onreadystatechange (index.html:83:27)

But I still correctly get the value on parsed.pswd

I can’t find the issue as the code works but it throws error anyway



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?