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

问题描述 投票:0回答:1

我找不到使用Javascript的Text Analytics 400 Bad Request Error问题的类似问题。 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");
});
javascript microsoft-cognitive
1个回答
0
投票

@Redz,看到这个小提琴实现。它可以很好地简单地替换你所说的“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));
            });

祝好运。会喜欢它听听它是怎么回事。

© www.soinside.com 2019 - 2024. All rights reserved.