javascript 从ChatGPT-API提取数据时出现CORS策略错误

lqfhib0f  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(229)

我正在尝试编写一个简单的JavaScript脚本,它使用ChatGPT API来提出问题并获得响应。
但是,我收到以下错误消息:
“CORS策略阻止了从源”https://wordpress-..... www.example.com“获取”https://api.chatgpt.com/answer?question = How%20are%20you?& api_key = sk-U3 BPK...“的访问权限cloudwaysapps.com:请求的资源上不存在“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.chatgpt.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.chatgpt.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>
cqoc49vn

cqoc49vn1#

ChatGPT API不存在yet

我们从ChatGPT研究预览中学到了很多,并根据用户反馈进行了重要更新。ChatGPT将很快应用于我们的API和Microsoft Azure OpenAI服务。
我很困惑。ChatGPT是如何通过API访问的呢?它不是。
您是指OpenAI API吗?如果是,请阅读documentation,尤其是Completions endpoint
这是正确的终点:

POST https://api.openai.com/v1/completions

相关问题