【发布时间】:2018-11-22 13:44:16
【问题描述】:
我正在尝试使用 Firebase REST API 将内容保存到我的数据存储中。我尝试使用 jQuery 和 vanilla JS XHR。但是,两者都给出相同的错误。 403 Bad Request 和这个响应:
无效数据;无法解析 JSON 对象、数组或值。也许您在键名中使用了无效字符。
这是我的 JSON 示例,我正在尝试保存:
{
"date": "2pm",
"name": "John"
}
这里是示例 ajax 请求:
jQuery.ajax({
accept: "application/json",
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "https://something.firebaseio.com/endpointnode.json",
data: {
"name": "John",
"date": "2pm"
},
});
请求的响应:
{
"error" : "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."
}
如您所见,没有特殊字符或任何东西。它应该可以工作。
它适用于 CURL 和 Httpie。我尝试检查 Httpie 中的-v 选项以获取详细信息。我把所有的标题都像 Httpie 一样。没有任何帮助。
顺便说一句,我的环境是可写的,所以应该没有任何权限问题。
知道如何实现吗?
谢谢。
【问题讨论】:
-
用响应编辑了帖子,就像这样:
{ "error" : "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names." }但在 httpie 中,它返回在 firebase 端创建的对象的 id,这是预期的行为:HTTP/1.1 200 OK Cache-Control: no-cache Content-Length: 31 Content-Type: application/json; charset=utf-8 Strict-Transport-Security: max-age=31556926; includeSubDomains; preload { "name": "-SomeRandomID" }