admin管理员组文章数量:1026630
I get this error when trying to make a request from react to my node server, even though I enabled cors so I dont understand why.
Error Message in the console:
Error message in the network tab:
react axios request:
try {
const url = "http://192.168.178.112:8080/api/users";
const { data: res } = await axios.post(url, data);
navigate("/login");
console.log(res.message);
} catch (error) {
if (
error.response &&
error.response.status >= 400 &&
error.response.status <= 500
)
setError(error.response.data.message);
}
node js express:
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
const connection = require("./db");
const userRoutes = require("./routes/users");
const authRoutes = require("./routes/auth");
connection();
app.use(express.json());
app.use(cors());
app.use("/api/users", userRoutes);
app.use("/api/auth", authRoutes);
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
I get this error when trying to make a request from react to my node server, even though I enabled cors so I dont understand why.
Error Message in the console:
Error message in the network tab:
react axios request:
try {
const url = "http://192.168.178.112:8080/api/users";
const { data: res } = await axios.post(url, data);
navigate("/login");
console.log(res.message);
} catch (error) {
if (
error.response &&
error.response.status >= 400 &&
error.response.status <= 500
)
setError(error.response.data.message);
}
node js express:
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
const connection = require("./db");
const userRoutes = require("./routes/users");
const authRoutes = require("./routes/auth");
connection();
app.use(express.json());
app.use(cors());
app.use("/api/users", userRoutes);
app.use("/api/auth", authRoutes);
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Share
Improve this question
asked Nov 16, 2024 at 17:10
robinnlmnrobinnlmn
311 silver badge10 bronze badges
1 Answer
Reset to default 1it does not seem like CORS problem, if you take a closer look at the browser logs, you'll see it says net::ERR_DISCONNECTED_INTERNET,
The net::ERR_DISCONNECTED_INTERNET error in JavaScript usually indicates that the browser has lost its connection to the internet. This error often occurs when trying to make network requests (e.g., using fetch, axios, or WebSocket) while the device is offline or there is a problem with the network.
I get this error when trying to make a request from react to my node server, even though I enabled cors so I dont understand why.
Error Message in the console:
Error message in the network tab:
react axios request:
try {
const url = "http://192.168.178.112:8080/api/users";
const { data: res } = await axios.post(url, data);
navigate("/login");
console.log(res.message);
} catch (error) {
if (
error.response &&
error.response.status >= 400 &&
error.response.status <= 500
)
setError(error.response.data.message);
}
node js express:
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
const connection = require("./db");
const userRoutes = require("./routes/users");
const authRoutes = require("./routes/auth");
connection();
app.use(express.json());
app.use(cors());
app.use("/api/users", userRoutes);
app.use("/api/auth", authRoutes);
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
I get this error when trying to make a request from react to my node server, even though I enabled cors so I dont understand why.
Error Message in the console:
Error message in the network tab:
react axios request:
try {
const url = "http://192.168.178.112:8080/api/users";
const { data: res } = await axios.post(url, data);
navigate("/login");
console.log(res.message);
} catch (error) {
if (
error.response &&
error.response.status >= 400 &&
error.response.status <= 500
)
setError(error.response.data.message);
}
node js express:
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
const connection = require("./db");
const userRoutes = require("./routes/users");
const authRoutes = require("./routes/auth");
connection();
app.use(express.json());
app.use(cors());
app.use("/api/users", userRoutes);
app.use("/api/auth", authRoutes);
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Share
Improve this question
asked Nov 16, 2024 at 17:10
robinnlmnrobinnlmn
311 silver badge10 bronze badges
1 Answer
Reset to default 1it does not seem like CORS problem, if you take a closer look at the browser logs, you'll see it says net::ERR_DISCONNECTED_INTERNET,
The net::ERR_DISCONNECTED_INTERNET error in JavaScript usually indicates that the browser has lost its connection to the internet. This error often occurs when trying to make network requests (e.g., using fetch, axios, or WebSocket) while the device is offline or there is a problem with the network.
本文标签: reactjsReact axios request to node express app gets blocked by corsStack Overflow
版权声明:本文标题:reactjs - React axios request to node express app gets blocked by cors - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745649545a2161231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论