Error when unit testing: “Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1”

后端 未结 4 1240
太阳男子
太阳男子 2020-12-10 16:58

I\'d like to unit test a service, however, when running the test, I get the following error:

Uncaught (in promise) SyntaxError: Unexpected token o in

4条回答
  •  忘掉有多难
    2020-12-10 17:41

    SyntaxError: Unexpected token o in JSON at position 1

    Reasons as per the error :

    • May be you are expecting Content-Type as JSON as a response from the API but you are getting response as a String start with alphabet o. Hence, It throws an error that Unexpected token o in JSON at position 1.
    • If the response header is text/html you need to parse, and if the response header is application/json it is already parsed for you.If you parse it again it will give you unexpected token o.

    If response is already a JSON Object then no need to use response.json().

提交回复
热议问题