[英]Javascript Call For Text Analytics Returned HTTP 400 Bad Request Error
對於使用Javascript的Text Analytics 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,有關實現,請參見此小提琴。 只需將您的密鑰替換為“ 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));
});
祝好運。 希望它聽到它對您的影響。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.