Моя задача - авторизация пользователя, при отправке данных через 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;
fetch('/auth/login' ...)
приведет Вас на Ваш же клиент. Или укажите полный адрес, или укажитеbaseUrl
(не знаю как это в fetch делается, но в том же axios лоика такая)