javascript - 生产模式下的 Angular 项目错误:SyntaxError: Unexpected token { in JSON at position 65
问题描述
我需要这方面的帮助。可能这是愚蠢的,但经过数小时的调查,我看不出问题所在。我有一个 Angular 10 项目。这是后端(php中的api):
...
$result = array(
'status' => 'success',
'code' => 200,
'data' => $productos
);
echo json_encode($result);
});
它返回这个 json:
{
"status":"success",
"code":200,
"data":
[
{
"id":"1",
"nombre":"Gazpacho tradicional",
"marca":"Hacendado",
"puntuacionYuca":"90\/100",
"puntuacionCoco":"9\/10",
"puntuacionMyReal":"Buen procesado",
"mediaVagana":"9",
"imagen":"gazpacho-hacendado.jpg",
"supermercado":"Mercadona",
"comentario":"Saludable en otras marcas"
}
]
}
我已经在几个网站上检查了 json,它是有效的。
这是角度 service.ts 中的代码:
getProductosVaganos(){
return this._http.get(this.urlvaganos+'productos-vaganos').pipe(map((response: any) => response.json()));
}
这是 angular component.ts 中的代码:
getProductosVaganos(){
this._productoVaganoService.getProductosVaganos().subscribe(
result => {
if (result.code != 200){
console.log(result);
}else{
this.productosVaganos = result.data;
}
},
error => {
console.log(<any>error);
}
);
}
它在本地开发模式下完美运行。但是当我编译项目并将其部署在主机的 public_html 文件夹中时,它会在 component.ts 函数中返回此错误:
list.component.ts > getProductosVaganos() > error: SyntaxError: Unexpected token { in JSON at position 65
at JSON.parse (<anonymous>)
at gd.json (main.99255eee2d008be2c609.js:1)
at O.project (main.99255eee2d008be2c609.js:1)
at O._next (main.99255eee2d008be2c609.js:1)
at O.next (main.99255eee2d008be2c609.js:1)
at XMLHttpRequest.s (main.99255eee2d008be2c609.js:1)
at l.invokeTask (polyfills.35a5ca1855eb057f016a.js:1)
at Object.onInvokeTask (main.99255eee2d008be2c609.js:1)
at l.invokeTask (polyfills.35a5ca1855eb057f016a.js:1)
at a.runTask (polyfills.35a5ca1855eb057f016a.js:1)
public_html 文件夹的结构与本地相同。
我已经测试了 JSON.stringify() 和 JSON.parse() 方法,但没有找到解决方案。我也测试过在 service.ts 的方法中删除行尾的 .json()。在这种情况下,当我在 chrome 中调试时,在生产中,它进入 component.ts 方法的“结果”内部,但 result.data 元素是“未定义”。
我认为问题出在本地和生产之间,因为它们有不同的语言、本地打字稿和生产中的编译脚本,即 javascript,但我不知道如何解决它。
任何帮助,将不胜感激。
问候。
解决方案
似乎response
得到了充分的回应。尝试{observe: 'body'}
如下显式编写。
getProductosVaganos(){
return this._http.get(this.urlvaganos+'productos-vaganos', {observe: 'body'}).pipe(map((response: any) => response.json()));
}
推荐阅读
- docker - 如何使用 iptables 允许 docker 使用特定端口 --> dport 在容器内使用端口
- javascript - Promise .catch 块中传递给函数的内容是什么?
- python - 我是否在 pytorch 中使用多项式回归?
- python - 将 udf 应用于多个列并使用 numpy 操作
- vue.js - 为什么 Nuxt Js 商店与 vue.js 不一样?
- c# - 值相同时不调用 C# wpf INotifyChanged
- ios - 如何使用 ionic3 从应用商店获取 iOS 部署的版本号
- javascript - 如何在 jquery 中访问 .env 文件?
- var - 正交脉冲响应的解释
- javascript - 读取 json 对象的属性时出现问题