【问题标题】:Sending POST REQUEST from FIREBASE isnt working从 FIREBASE 发送 POST 请求不起作用
【发布时间】:2020-03-31 15:02:00
【问题描述】:

我试图从我的 firebase 函数发送一个发布请求,但该发布请求从未运行。在 firebase 控制台中,一切似乎都很好。

		var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
		var xhttp = new XMLHttpRequest();
		xhttp.open("POST", url, true);
		xhttp.setRequestHeader("Content-type", "application/json");
		xhttp.setRequestHeader("Authorization", "key="+ key);
		let res = {
			"notification": {
				"title": "Test",
				"body": "Notificación enviada desde Firebase"
			},
			"to": to,
		}
		xhttp.send(JSON.stringify(res))
		console.log(xhttp)
		return (xhttp);

我不知道我做错了什么

【问题讨论】:

标签: javascript firebase post xmlhttprequest google-cloud-functions


【解决方案1】:

我建议您在代码中实现请求完成时的逻辑,如here 所述,这样您就可以调试您尝试发送此请求的服务器正在发送的内容。

最后,如果你不是很熟悉 XMLHttpRequest,你可以在你的 firebase 函数(基于 Promise 的 HTTP 客户端)中尝试 axios,

axios.post('/your_endoint', {
    param1: 'hello',
    param2: 'there'
})
.then(function (response) {
    console.log(response);
})
.catch(function (error) {
    console.log(error);
});

要查看 axios 配置的更多详细信息,您可以访问 Github page 详细信息

【讨论】:

    最近更新 更多