Javascript 调用文本分析返回 HTTP 400 错误请求错误

标签 javascript azure-cognitive-services

我找不到使用 Javascript 的文本分析 400 错误请求错误问题的类似问题。下面的 Javascript 代码返回了一个错误。我已验证发送的 POST 有效。我需要您的帮助。

var params = {
    "documents": [
        {
            "language": "en",
            "id": "1",
            "text": "This is my first test. All is good"
        },
        {
            "language": "en",
            "id": "2",
            "text": "This is my first test. All is not good"
        },
        {
            "language": "en",
            "id": "3",
            "text": "Why is this not working as expected?"
        },
        {
            "language": "en",
            "id": "4",
            "text": "You got to be kidding me like this"
        },
        {
            "language": "en",
            "id": "5",
            "text": "I hope this will finally work. I hope it will"
        }
    ]
}

$.ajax({
    url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment?" + $.param(params),
    beforeSend: function(xhrObj){
        // Request headers
        xhrObj.setRequestHeader("Content-Type","application/json");
        xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","MY API KEY");
    },
    type: "POST",
    // Request body
    data: "{body}",
})
.done(function(data) {
    console.log(data);
})
.fail(function() {
    alert("error");
});

最佳答案

@Redz, 请参阅此 fiddle 以了解实现情况。只需将您的 key 替换为“YOUR_KEY_HERE”即可:https://jsfiddle.net/expcc0f5/1/

以下是成功运行的代码:

var params = {
                "documents": [
                    {
                        "language": "en",
                        "id": "1",
                        "text": "This is my first test. All is good"
                    },
                    {
                        "language": "en",
                        "id": "2",
                        "text": "This is my first test. All is not good"
                    },
                    {
                        "language": "en",
                        "id": "3",
                        "text": "Why is this not working as expected?"
                    },
                    {
                        "language": "en",
                        "id": "4",
                        "text": "You got to be kidding me like this"
                    },
                    {
                        "language": "en",
                        "id": "5",
                        "text": "I hope this will finally work. I hope it will"
                    }
                ]
            };

            $.ajax({
                 method: 'POST',
                url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment",
                headers:{
                    "Content-Type":"application/json",
                  "Ocp-Apim-Subscription-Key":"YOUR_KEY_HERE",
                  "Accept":"application/json"
                },
                data: JSON.stringify(params),
                dataType: 'text',
            })
            .done(function(data) {
                console.log('Here: ' + data);
                $('#responseData').html(data);
            })
            .fail(function(data) {
                alert("error" + JSON.stringify(data));
            });

祝你好运。我很想听听你的情况如何。

关于Javascript 调用文本分析返回 HTTP 400 错误请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41726846/

相关文章:

c# - 使用 Azure Read/OCR API 从 PDF 中提取复选框数据

azure - 如何在 Azure 认知搜索中获取分页的不同值?

javascript - 如何从组件容器中设置 React 组件的样式?

javascript - 为什么在 jQuery 中同时调用 if 和 else 条件?

microsoft-cognitive - 显示 QnAMaker 后续提示的文本

azure - Terraform 部署 Azure 认知帐户 OpenAi 'The subscription does not have QuotaId'

javascript - $.getJSON 不触发 console.log

javascript - Angular JS 指令卸载事件或等效事件

javascript - 为什么函数名在分配给变量后不可用

azure - 无法使用 Azure Face API - 禁止错误