Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse () at Response.Body.json

前端 未结 6 2361
情书的邮戳
情书的邮戳 2021-02-15 14:16

I am working on an angular2 project. I am stuck with these errors. The error occured when I tried to send the JSON objects to the backend. It may be due the parsing of JSON obje

6条回答
  •  無奈伤痛
    2021-02-15 14:44

    Thanks @Jacob Krall for pointing out the reason:

    I was getting the same error for following code

    this.http.post(URL, formData).map((res: Response) => res.json()).subscribe(
    (success) => {
        alert(success);
    },
    (error) => alert(error))
    

    Reason: I was not sending json data from server itself so it was crashing for line res.json()

    Solution: Return json response from server then it should work fine.

    replaced the following

    return res.send("Upload Completed for " + path);
    

    with,

    return res.json("Upload Completed for " + path);
    

提交回复
热议问题