gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:13:05 25 4
gpt4 key购买 nike

我找不到使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com