Uncaught (in promise) SyntaxError: Unexpected end of JSON input

前端 未结 4 1348
独厮守ぢ
独厮守ぢ 2020-12-18 01:47

I am trying to send a new push subscription to my server but am encountering an error \"Uncaught (in promise) SyntaxError: Unexpected end of JSON input\" and the console say

4条回答
  •  無奈伤痛
    2020-12-18 02:11

    For someone looking here later. I received this error not because of my headers but because I was not recursively appending the response body to a string to JSON.parse later.

    As per the MDN example (I've taken out some parts of their example not immediately relevant):

    reader.read().then(function processText({ done, value }) {
        if (done) {
          console.log("Stream complete");
          return;
        }
        result += chunk;
        return reader.read().then(processText);
      });

    For my issue I had to

    1. Use a named function (not an anonymous ()=>{}) inside the .then
    2. Append the result together recursively.
    3. Once done is true execute something else on the total appended result

    Just in case this is helpful for you in the future and your issue is not header related, but related to the done value not being true with the initial JSON stream response.

提交回复
热议问题