Google自然语言REST API返回错误400收到的无效JSON有效内容。未知名称\“document \”:“

时间:2017-10-16 10:25:51

标签: ajax rest nlp google-cloud-platform

我使用谷歌自然语言来检测实体情绪,向https://language.googleapis.com/v1/documents:analyzeEntitySentiment发送ajax调用总是返回错误400,我的ajax调用如下,

{

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."
      }
    ]
  }
]

如文档https://cloud.google.com/natural-language/docs/reference/rest/v1/documents/analyzeEntitySentiment中所述,“document”应该用作请求正文数据。

提前致谢!

1 个答案:

答案 0 :(得分:1)

删除文档上的JSON.Stringify调用。您的有效负载已经是字符串格式。

 data        : {
                    "document": {  "type":"PLAIN_TEXT",
                                      "content":"Nature is so beautiful" 
                                  },
                    "encodingType":"UTF8" 
                },