简体   繁体   中英

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. A straightforward code below in Javascript returned an error. I've verified that the POST sent was valid. 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. It works well simply replace your key where it says "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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