【问题标题】:Ajax post request to google NLPAjax 向谷歌 NLP 发布请求
【发布时间】:2018-01-25 00:30:17
【问题描述】:

我正在尝试向GCP Natural Language 发送帖子请求以进行情绪分析。

当我在谷歌的代码浏览器上尝试数据格式时,它工作正常,但是当我在 html 页面上运行它时,我得到一个错误,显示为

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"document[type]\": Cannot bind query parameter. Field 'document[type]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"document[content]\": Cannot bind query parameter. Field 'document[content]' could not be found in request message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"document[type]\": Cannot bind query parameter. Field 'document[type]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"document[content]\": Cannot bind query parameter. Field 'document[content]' could not be found in request message."
          }
        ]
      }
    ]
  }
}

我的代码是:

<!DOCTYPE html>
<html>
<body>

<h1> Testing sentiment Analysis </h1>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    var APIKEY = "AN API KEY";

    $.ajax({
        type        : "POST",
        url         : "https://language.googleapis.com/v1/documents:analyzeSentiment?key="+APIKEY,
        data        :   {
                            "document": {
                                "type": "PLAIN_TEXT",
                                "content": "Hello I am great"
                            },
                            "encodingType": "UTF8",
                        },
        success: function(res) {
                console.log(res);
                alert(res['message']);
        },
        error: function(res) {
                console.log(res['message']);
                alert(res);
        },
    });
</script>
</body>
</html>

【问题讨论】:

  • 我已经更新了答案。你能查一下吗?

标签: json ajax api google-cloud-platform sentiment-analysis


【解决方案1】:

更新: 一位同事向我指出了我所犯的错误。我们必须使用JSON.stringify() 才能发送请求。代码应该是这样的:

$.ajax({
    type        : "POST",
    url         : "https://language.googleapis.com/v1/documents:analyzeEntitySentiment?key=YOUR-API-KEY", 
    contentType : "application/json; charset=utf-8",
    data        : 
                    JSON.stringify({
                        "document": {
                            "type": "PLAIN_TEXT",
                            "language": "en",
                            "content": "Hola Victor"

                        },
                        "encodingType": "UTF8"}),
    success     : function(_result){ 

        if (_result) { 
            alert('SUCCESS');
        } else {
            alert('ERROR');
        }
    },

    error       : function(_result){

    }
});

我已经对其进行了测试,并且可以正常工作。

【讨论】:

  • 感谢您的回答维克多。我仍然收到错误"message": "Invalid JSON payload received. Unknown name \"document[type]\": Cannot bind query parameter. Field 'document[type]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"document[content]\": Cannot bind query parameter. Field 'document[content]' could not be found in request message.",
  • 我已经让它与 Curl 和 Postman 一起工作。出于某种原因,它使用 ajax 失败
  • 你的 CURL 代码是什么? (由于管理问题,无法在我的电脑上安装邮递员)
  • 创建一个名为 request.json 的文件,其内容为:{ "document": { "content": "Hello dear", "language": "en", "type": "PLAIN_TEXT" }, "encodingType": "UTF8" },并在同一文件夹中运行 curl:curl -v -s -H "Content-Type: application/json" \ https://language.googleapis.com/v1/documents:analyzeSentiment?key=YOUR-API-KEY \ --data-binary @request.json
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 2018-07-04
相关资源
最近更新 更多