错误:
未处理的 promise 拒绝错误:“网络错误”
我的前端 vue 应用程序在 http://localhost:8080
中运行,后端在 http://localhost:8082
中运行
我正在请求从 axios 从 vue 文件到后端的 get 请求。
onSubmit(){
return axios.get(`http://192.168.43.35:8082/auth/$(this.email)-$(this.password)`).then(function(response){console.log(response)}).catch(function(error){
throw error;
console.log(error);
});
这是我的端点
app.get('/auth/:email-:password',cors(),(req,res)=>{
MongoClient.connect(url, function(err, db) {
if (err) console.log(err);
var dbo = db.db("leaveautomation");
dbo.collection("letters").find({email:req.params.email, password:req.params.password},{ projection: { _id: 0, email: 1,} }).toArray(function(err, result) {
if (err) console.log(err);
res.send({
email: result,
length: result.length
});
db.close();
});
});})
这个错误的原因可能是什么!?
最佳答案
您是否将原始 header 添加到服务器端的中间件中?
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'Accept,X-Requested-With,Origin,Content-Type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
关于node.js - 未处理的 promise 拒绝错误: "Network Error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53169942/