简体   繁体   English

文本分析的Java语言调用返回HTTP 400错误请求错误

[英]Javascript Call For Text Analytics Returned HTTP 400 Bad Request Error

I can't find a similar question for Text Analytics 400 Bad Request Error issue using Javascript. 对于使用Javascript的Text Analytics 400错误请求错误问题,我找不到类似的问题。 A straightforward code below in Javascript returned an error. 以下Javascript中的简单代码返回了错误。 I've verified that the POST sent was valid. 我已验证发送的POST是否有效。 I need your kind help. 我需要你的帮助。

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, See this fiddle for implementation. @Redz,有关实现,请参见此小提琴。 It works well simply replace your key where it says "YOUR_KEY_HERE" : https://jsfiddle.net/expcc0f5/1/ 只需将您的密钥替换为“ YOUR_KEY_HERE”即可: https ://jsfiddle.net/expcc0f5/1/

Following is the code that works successfully: 以下是成功运行的代码:

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));
            });

Good Luck. 祝好运。 Would love it hear how it went for you. 希望它听到它对您的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM