Google自然语言REST API返回错误400收到无效的JSON有效内容。未知名\“文件\”:“
问题描述:
我使用谷歌自然语言来检测实体情绪,发送Ajax调用https://language.googleapis.com/v1/documents:analyzeEntitySentiment总是返回错误400,我的Ajax调用如下,Google自然语言REST API返回错误400收到无效的JSON有效内容。未知名“文件”:“
{
APIKEY = '**********************';
$.ajax({
type : "POST",
url : "https://language.googleapis.com/v1/documents:analyzeEntitySentiment?key="+APIKEY,
ContentType : "application/json",
data : {
"document": JSON.stringify(
{ "type":"PLAIN_TEXT",
"content":"Nature is so beautiful"
}),
"encodingType":"UTF8"
},
success : function(_result){
if (_result) {
alert('SUCCESS');
}else{
alert('ERROR');
}
},
error : function(_result){
alert(_result);
}
});
和错误:如在实况提到
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"document\": Cannot bind query parameter. 'document' is a message type. Parameters can only be bound to primitive types.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"document\": Cannot bind query parameter. 'document' is a message type. Parameters can only be bound to primitive types."
}
]
}
]
应使用“文档”作为请求主体数据。
提前致谢!
答
删除文档上的JSON.Stringify调用。你的有效载荷已经是字符串格式。
data : {
"document": { "type":"PLAIN_TEXT",
"content":"Nature is so beautiful"
},
"encodingType":"UTF8"
},