node.js - 我收到错误“位置的 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>↵<html lang="en">↵<head>↵ <meta ch…ipt src="main.js" defer></script></body>↵</html>↵"}
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"
解决方案
推荐阅读
- css - css没有将样式应用于第n个孩子的范围
- html - 当我添加另一个
- 对我
- 它一直出现在页面底部的容器外部
- 对我
- javascript - 如何用新输入绘制新弹丸?
- c# - 使用 Utf8Json 库进行序列化时排除空字段?
- python - Tensorflow 提供不同的控制台输出,并且由于某种原因速度较慢
- wordpress - Wordpress 联系表 -7 和 Pardot 集成
- jquery - JQuery 在更改和延迟时运行功能
- python - json.decoder.JSONDecodeError:期待','分隔符:python中的错误
- react-native - 迭代以显示图像
- python - 从 Django 中的 QuerySet 中排除对象实例的最佳方法是什么(在这种情况下 filter() 不是一个选项)?