JavaScript POST 提取作为选项发送
JavaScript POST fetch sends as OPTIONS
我正在学习JavaScript,我正在制作一个帐户登录页面作为一个学习项目。服务器正在使用 Python 的 Flask。 fetch 函数似乎没有像我希望的那样工作。其一,它发送的是选项请求而不是 POST,即使我指定了 POST。另一件事是服务器没有接收到数据,它显示为空白。这是代码:
var content = JSON.stringify({'username': username, 'password': password}) //username and password are user inputs, I had them print and these work fine.
var response = fetch(url+"/api/login", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: content
})
.then(response => {
console.log("Success") //this doesn't print.
})
那是一个 preflight 请求
通过返回正确的 CORS Headers 来处理选项请求。
然后还要处理 post 请求,由于 API 和网站的新 CORS 标准,这两个请求都是必需的。
我正在学习JavaScript,我正在制作一个帐户登录页面作为一个学习项目。服务器正在使用 Python 的 Flask。 fetch 函数似乎没有像我希望的那样工作。其一,它发送的是选项请求而不是 POST,即使我指定了 POST。另一件事是服务器没有接收到数据,它显示为空白。这是代码:
var content = JSON.stringify({'username': username, 'password': password}) //username and password are user inputs, I had them print and these work fine.
var response = fetch(url+"/api/login", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: content
})
.then(response => {
console.log("Success") //this doesn't print.
})
那是一个 preflight 请求
通过返回正确的 CORS Headers 来处理选项请求。
然后还要处理 post 请求,由于 API 和网站的新 CORS 标准,这两个请求都是必需的。