У меня есть вопрос. В стороне клиента, я использую выборки API, чтобы получить ответ, это кодИзвлечение не может получить ответ от сервера
window.fetch('/signup', {
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: formDataStr,
credentials: 'same-origin'
})
.then(function (response) {
return response.json()
})
.then(function (result) {
if (result.code === 0) {
return this.$router.push('/main')
}
})
.catch((err) => {
self.isLoading = false
console.log(err)
})
На сервере, написано в узле и выразить это код:
`app.post('/login', function (req, res) {
var loginEmail = req.body.email2
var loginPsd = req.body.secret
UserDataModel.findOne({ email: loginEmail }, {
name: 1,
password: 1,
email: 1
}, function (err, userInfo) {
if (err) {
console.log(err)
}
if (!userInfo) {
res.send({
code: -1,
msg: 'user not exist'
})
return console.log('user not exist')
}
userInfo.comparePassword(loginPsd, function (err, isMatch) {
if (err) {
console.log(err)
}
if (isMatch) {
req.session.isLogin = true
req.session.userInfo = {
userName: userInfo.name,
userEmail: userInfo.email
}
console.log(req.session)
res.json({
code: 0,
msg: 'login succeed!'
})
res.end()
} else {
res.send({
code: -2,
msg: 'wrong password'
})
}
})
})
})`
Консоль показывает, что сообщение успешно с кодом статуса 200. Однако выбор, следующий после этого, не может выполнить. Кажется, что от сервера нет ответа.
это как Цепочки Promise. возвращенный Promise разрешается в следующий '.then' –
фактически, когда я запускаю программу, этот код' .then (функция (ответ) { return response.json() }) 'не выполняется, сервер успешно получает данные от клиента, но клиент не смог получить ответ от сервера, я много раз пробовал. – zzy