node.js - Firebase 函数获取文本响应错误“JSON 中的意外令牌 H”
问题描述
我试图找出这个错误:
"error: SyntaxError: Unexpected token H in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:8646:51) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2743:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:38114:33) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2742:36) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:4200/polyfills.js:2510:47) at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:2818:34) at invokeTask (http://localhost:4200/polyfills.js:3862:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:3888:17)
text: "Hello from Firebase!"
这是我的功能:
exports.helloWorld = functions.https.onRequest((req, res) => {
cors(req, res, () => {
res.send("Hello from Firebase!");
});
});
这是我对该函数的调用:
public sendHttpRequestTest(){
let url = `my_link_to_functions/helloWorld`;
const req = new HttpRequest("GET",url);
this.http.request(req).toPromise()
.then( res => {
console.log(res);
})
.catch( err => {
console.log(err);
})
}
解决方案
我通过将其作为 JSON 发送来修复它,如下所示:
res.send(JSON.stringify("Hello from Firebase!"));
推荐阅读
- unity3d - 将 3d 对象与设备的陀螺仪统一对齐
- push-notification - 观看 Google 日历推送通知时出现错误“WEB_HOOK 频道不可用”
- vba - Excel vba按每n行排序并删除重复项
- android - 如何在 Android 和 iOS 应用程序中使用 WhatsApp 登录?
- python - 在 Python 中的列表上应用 if-else 单行
- laravel - tymon/jwt-auth Laravel:无法验证令牌签名
- mysql - 在 laravel 中授予超级管理员对客户和员工的全局访问权限
- javascript - 使用 .text 时,它返回未定义的值
- c# - 来自动态加载的 XAML 的触发事件
- node.js - swaggerhub 和 REST API