admin管理员组文章数量:1326285
I am learning JavaScript, and I am making an account log-in page as a learning project. The server is using Python's Flask. The fetch function doesn't seem to be working as I want it to. For one, it is sending an options request instead of a POST, even though I specified POST. Another thing is that the server isn't receiving the data, it es up as blank. Here is the code:
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.
})
I am learning JavaScript, and I am making an account log-in page as a learning project. The server is using Python's Flask. The fetch function doesn't seem to be working as I want it to. For one, it is sending an options request instead of a POST, even though I specified POST. Another thing is that the server isn't receiving the data, it es up as blank. Here is the code:
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.
})
Share
Improve this question
edited Jul 8, 2023 at 8:21
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Nov 16, 2021 at 17:11
helleelhelleel
431 silver badge5 bronze badges
2
- 1 So sounds like you are making a cross domain call. It is doing the handshake before it posts. – epascarello Commented Nov 16, 2021 at 17:13
- stackoverflow./questions/38375124/… – epascarello Commented Nov 16, 2021 at 17:15
3 Answers
Reset to default 5thats a preflight request
Handle the options request by passing the correct CORS Headers back.
Then also handle the post request, both of these are needed due to the new CORS standards for APIs and Websites.
Make sure to use cors if you want a proper response... Here's how I would do it. The request was fine but the .then() was not good for console.logging...
var response = fetch(url+"/api/login", {
method: "POST",
mode: "cors",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: content
})
.then(r => r.text().then(console.log));
I found the same problem and solved it by adding mode: "no-cors"
to the options dictionary, as in:
var response = fetch(url+"/api/login", {
method: "POST",
mode: "no-cors",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: content
})
.then(response => {
console.log("Success") //this doesn't print.
})
本文标签: httpJavaScript POST fetch sends as OPTIONSStack Overflow
版权声明:本文标题:http - JavaScript POST fetch sends as OPTIONS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742210325a2433592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论