我正在跟踪this tutorial from Hakernoon,以将我的机器用作本地服务器来使用MySQL设置Node.js。 我正在使用一些不同的工具,但它们似乎不是问题。我正在使用:
*(Ubuntu和Workbench是唯一的区别)
总而言之,除了我进入“登录”部分之外,整个教程都运行良好。在这里,我们构建了一个表单,用于输入发出POST
请求的用户名和密码。然后,如果一切正常,则返回200 status
,否则显示一条消息,指示登录失败。这就是问题所在。每次尝试登录时(即使确保凭据正确并且知道该用户存储在数据库中),也会告诉我登录失败。
在浏览器检查工具上检查时,它显示一个Fail to load resources: the server responded with a status of 404 (Not found)
。它还指出问题在于我在fetch()
函数中使用的post
函数中发出了POST
请求。
我检查了fetch()
上的文档,但并不清楚。我也尝试与本教程的作者联系,但我没有任何答案。为获取404状态,获取时我在做什么呢?
此外,每次我创建一个新用户(也是一个POST
请求)时,它都不会出现问题,并且可以在MySQLWorkbench的数据库中看到它。
这是文件(app.js),其中包含用于创建用户和登录的代码:
const CreateUser = document.querySelector('.CreateUser')
CreateUser.addEventListener('submit', (e) => {
e.preventDefault()
const username = CreateUser.querySelector('.username').value
const password = CreateUser.querySelector('.password').value
post('/createUser', { username, password })
})
const Login = document.querySelector('.Login')
Login.addEventListener('submit', (e) => {
e.preventDefault();
const username = Login.querySelector('.username').value
const password = Login.querySelector('.password').value
post('/login', {username, password})
.then(({status}) => {
if(status === 200)
alert('login success')
else
alert('login failed')
})
})
function post (path, data) {
return window.fetch(path, { <========= Here is where the browser shows the problem
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
}
***我没有发布剩余的代码,以免使其过长,如果需要更多信息,请告诉我)。谢谢!