Ответ с сервера приходит в виде HTML, а не JSON
Моя задача - авторизация пользователя, при отправке данных через fetch я получаю HTML, а не JSON
Vue + Nuxt
methods: {
async sendData() {
try {
const values = { name: 'Alex', password: 123456 };
const res = await this.$store.dispatch('auth/login', values);
console.log(await res);
} catch(err) {
throw err;
}
}
}
Vuex
export const actions = {
async login({}, data) {
try {
const res = await fetch('/auth/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
return await res.json();
} catch (err) {
throw err;
}
}
}
Node.js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const authRouter = require('./routes/auth');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use('auth', authRouter);
module.exports = app;
Роутер auth.js
const express = require('express');
const router = express.Router();
router.post('/login', (req, res) => {
const { name, password } = req.body;
return res.json({
password,
name
});
});
module.exports = router;
Ответы (1 шт):
Автор решения: Alexandr_Yakovlev
→ Ссылка
Решил проблему, она заключалась в том, что не было поставлено слэша "/", когда я использовал роутер
app.use('/auth', authRouter);