Firebase REST API POST请求失败,并显示错误:“数据无效;无法解析JSON对象,数组或值...“ [英] Firebase REST API POST request fails with error: "Invalid data; couldn't parse JSON object, array, or value..."

查看:225
本文介绍了Firebase REST API POST请求失败,并显示错误:“数据无效;无法解析JSON对象,数组或值...“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Firebase REST API将内容保存到我的数据存储中。我试着用jQuery和香草JS XHR。但是,都给出了同样的错误。 403错误的请求和这个响应:

lockquote

无效的数据;无法解析JSON对象,数组或值。或许您在密钥名称中使用了无效字符。


这是我的JSON示例, p>

  {
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:{
名称:John,
date:2pm
},
});

请求的回应:

  {
error:无效的数据;无法解析JSON对象,数组或值,或许您在密钥名称中使用了无效字符。





$ b

正如你所看到的,没有特殊的字符或任何东西。它应该只是工作。

它适用于CURL和Httpie。我试图检查Httpie中的 -v 选项以获取详细信息。我把所有的头像Httpie所做的。没有帮助。
顺便说一句,我的环境是可写的,所以不应该有任何权限问题。



任何想法如何实现这个?

$ b $

解决方案

您已指定您的 AJAX request通过赋值 contentType 属性包含 json 字符串。但是,附加请求的参数不是JSON字符串。为了使数据成为json字符串,只需调用 JSON.stringify(params)方法。



可以帮助你解决问题。

  var data = {name:John,date:2pm }; 
jQuery.ajax({
accept:application / json,
type:'POST',
contentType:application / json; charset = utf-8,
dataType:json,
url:https://something.firebaseio.com/endpointnode.json,
data:JSON.stringify(data),
}) ;

干杯。


I'm trying to use Firebase REST API to save stuff into my data store. I tried with jQuery and vanilla JS XHR. However, both give the same error. 403 Bad Request and this response:

Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names.

Here is my example JSON, I'm trying to save:

{
    "date": "2pm", 
    "name": "John"
}

Here is the example ajax request:

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"        
    },
});

Response of the request:

{
    "error" : "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."
}

As you can see, there is no special chars or anything. It should just work.

It works fine with CURL and Httpie. I tried to check -v option in Httpie for details. I put all of the headers as Httpie does. Nothing helped. By the way, my environment is writable so there should not be any permission issue.

Any idea how to achieve this?

Thanks.

解决方案

You have specified that your AJAX request contains json string by assigning contentType property. However, the paramaters attached to request is not JSON string. In order to make data as json string, just invoke JSON.stringify(params) method.

Following code snippet can assist you to solve the problem.

var data = {"name": "John", "date": "2pm"};
jQuery.ajax({
    accept: "application/json",
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    url: "https://something.firebaseio.com/endpointnode.json",
    data: JSON.stringify(data),
});

Cheers.

这篇关于Firebase REST API POST请求失败,并显示错误:“数据无效;无法解析JSON对象,数组或值...“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆