用Python写了个用于调用openai接口学习本地Mysql数据库并根据用户的问题进行回答的程序。
现在写了个html页面来对这个程序进行测试,我在html输入提交问答的问题,python程序会接收到问题。
问题:Python程序无法接收到来自HTML页面的问题,一开始查到是跨域的问题按网上说的用CORS解决。一开始是可以的,不知道为什么突然有一天又不能跨域了(完全没有修改程序),问问大家这可能是什么原因,需要这么解决?
浏览器报错↓
Python程序片段↓
HTML页面代码片段↓
function sendQuestion() {
var question = document.getElementById("question").value;
appendUserMessage(question);
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://10.xxx.x.6:5000", true);//**Python服务器地址**
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
appendBotMessage(response.response);
}
};
var data = JSON.stringify({ "question": question });
xhr.send(data);
document.getElementById("question").value = "";
}
CORS 需要后端加头部 但并不是所有浏览器都支持
解决方案,参考下面