首页 > 解决方案 > 我收到错误“位置的 JSON 中出现意外的令牌...”

问题描述

我的蒙戈 JSON:

{"_id":{"$oid":"5f9e0bebd5c7e2fdf70f1f58"},
"formAttributes":[
{"attributeName":"firstName","attributeType":"text"},
{"attributeName":"lastName","attributeType":"date"}
]}

当我只运行我的 nodejs api 时,它会正确给出结果。

route.get('/all', (req, res) =>{
    FormAttributes.find((error, data) =>{
        if(error){
            console.log(error);
        }
        else{
            res.send(data);
        }
    });
});

当我尝试从 Angular 前端执行此操作时,出现错误。

    this.sharedService.getAttributes().subscribe((res) => {
      console.log(res)
      });
    }

我的服务:

  
  getAttributes(){
    const headerDict = {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Access-Control-Allow-Headers': 'Content-Type',
    }
    const requestOptions = {                                                                                                                                                                                 
      headers: new HttpHeaders(headerDict), 
    };

    return this.http.get('formattributes/all', requestOptions);
  }

我不会在任何地方自己解析 JSON。为什么我会看到此错误:

error: {error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHtt…, text: "<!doctype html>↵&lt;html lang="en">↵&lt;head>↵  <meta ch…ipt src="main.js" defer></script></body>↵&lt;/html>↵&quot;}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:8080/formattributes/all"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:8080/formattributes/all"

标签: node.jsjsonangular

解决方案


推荐阅读