Я получаю ошибки:
localhost/:1 Failed to load http://localhost:5000/api/hello: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Это мой асинхронный POST:
async function submitToServer(data){
try{
let response = await fetch('http://localhost:5000/api/hello', {
method: 'POST',
headers: {
'Content-type' : 'application/json',
},
body: JSON.stringify(data),
});
let responseJson = await response.json();
return responseJson;
} catch (error) {
console.error(error);
}
}
А это мой сервер:
const express = require('express');
const app = express();
const port = process.env.PORT || 5000;
app.get('/api/hello', (req, res) => {
res.send({ express: 'Hello From Express' });
});
app.listen(port, () => console.log(`Listening on port ${port}`));
Что мне делатьотправить эту информацию в API?
Нужно ли создавать конечную точку или что-то еще?
Итак, я установил cors npm
.
Теперь у меня есть следующие ошибки:
POST localhost: 5000 / api / hello 404 (Not Found)
и
SyntaxError: Неожиданный токен <в JSON в позиции 0 </p>
Что я могу сделать сейчас?