错误:
Unhandled promise rejection Error: "Network Error"
我的前端vue应用程序在http://localhost:8080
中运行,而后端在http://localhost:8082
中运行
我正在要求axios从vue文件到后端获取请求。
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();
});
});})
此错误的原因可能是什么??
答案 0 :(得分:0)
您是否将原始标头添加到服务器端的中间件中?
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();
});