Я пытаюсь написать простой сценарий JavaScript, который использует API
Однако я получаю следующее сообщение об ошибке:
"Доступ к извлечению в 'https://api.' из источника 'https://wordpress-......cloudwaysapps.com' был заблокирован политикой CORS: заголовок «Access-Control-Allow-Origin» отсутствует. присутствует на запрашиваемом ресурсе. Если непрозрачный ответ служит вашему потребности, установите режим запроса «no-cors», чтобы получить ресурс с помощью CORS отключен."
Я включил заголовки CORS на стороне сервера в своей среде хостинга. Но ошибка остается.
В чем причина этой проблемы и как я могу решить эту проблему?
Вот мой код:
<html>
<head>
<script>
function askQuestion() {
var question = document.getElementById("questionInput").value;
var apiKey = document.getElementById("apiKey").value;
// access chatgpt's API and pass in the question and API key as parameters
fetch("https://api.com/answer?question = " + question + "&api_key = " + apiKey)
.then(response => {
if (!response.ok) {
throw new Error("Failed to fetch answer from API");
}
return response.json();
})
.then(data => {
// get the answer from the API response and display it in the textbox
document.getElementById("answerBox").value = data.answer;
})
.catch(error => {
console.error("Error fetching answer from API: ", error);
});
}
function askFollowUpQuestion() {
var followUpQuestion = document.getElementById("followUpQuestionInput").value;
var apiKey = document.getElementById("apiKey").value;
// access chatgpt's API and pass in the follow-up question and API key as parameters
fetch("https://api.com/answer?question = " + followUpQuestion + "&api_key = " + apiKey)
.then(response => {
if (!response.ok) {
throw new Error("Failed to fetch answer from API");
}
return response.json();
})
.then(data => {
// get the answer from the API response and display it in the textbox
document.getElementById("followUpAnswerBox").value = data.answer;
})
.catch(error => {
console.error("Error fetching answer from API: ", error);
});
}
</script>
</head>
<body>
<input type = "text" id = "questionInput" placeholder = "Enter your question here"></input>
<br>
<input type = "text" id = "apiKey" placeholder = "Enter your API key"></input>
<br>
<button onclick = "askQuestion()">Ask</button>
<br>
<textarea id = "answerBox" readonly></textarea>
<br>
<input type = "text" id = "followUpQuestionInput" placeholder = "Enter your follow-up question here"></input>
<br>
<button onclick = "askFollowUpQuestion()">Ask Follow-up</button>
<br>
<textarea id = "followUpAnswerBox" readonly></textarea>
</body>
</html>
chatgpt определенно официально не поддерживается openai API. Хотя это уже в пути.
Мы многое узнали из предварительного исследования ChatGPT и были внесение важных обновлений на основе отзывов пользователей. ChatGPT будет скоро появится в нашем API и Microsoft Azure OpenAI Service.
Я в замешательстве. Как
Вы имели в виду OpenAI API? Если да, то прочтите документацию , особенно конечную точку Completions.
Это правильная конечная точка:
POST https://api.openai.com/v1/completions
Где вы нашли
https://api.chatgpt.com
? Было ли это предоставлено ChatGPT?