我正在使用reqres应用程序接口进行登录,但它是400错误,上面写着
{
"error": "Missing email or username"
}
我尝试使用postman,但同样的错误,这是来自postman的URL字符串
https://reqres.in/api/login?email=eve.holt@reqres.in&password=cityslicka
我在JS中使用了以下代码
form.addEventListener("submit", function(e){
const myData = 'email=eve.holt@reqres.in&password=cityslicka'
e.preventDefault();
fetch("https://reqres.in/api/login", {
method: 'post',
headers : {"Content-Type": "application/x-www-form-urlencoded"} ,
body: {
myData
},
})
})
发布于 2020-09-17 00:27:01
由于您正在发出POST请求,因此需要在请求正文中发送数据,而不是在URL中发送数据。
发布于 2020-09-17 04:38:01
事实证明我做错了,但在遵循这个video之后让它工作了,正确的(但硬编码的)代码如下
form.addEventListener("submit", function(e){
e.preventDefault();
fetch("https://reqres.in/api/login", {
method: 'POST',
headers : {"Content-Type": "application/json"} ,
body: JSON.stringify({
"email":"eve.holt@reqres.in",
"password":"cityslicka"
})
})
.then(res => res.json())
.then(data => console.log(data))
})
感谢@Code-Apprentice提供的指针
https://stackoverflow.com/questions/63924273
复制相似问题