2017-10-16 43 views
0

我使用谷歌自然語言來檢測實體情緒,發送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." 
     } 
    ] 
    } 
] 

應使用「文檔」作爲請求主體數據。

提前致謝!

回答

1

刪除文檔上的JSON.Stringify調用。你的有效載荷已經是字符串格式。

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