node.js - 未处理的 promise 拒绝错误: "Network Error"

标签 node.js express vue.js routes axios

错误:
未处理的 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/

相关文章:

vue.js - Vuex,全局错误和通知处理的最佳实践

node.js - Node 中的自定义错误代码应使用什么范围?

javascript - Node.js 生成的 csv 文件显示英镑符号 (£)

javascript - React Express 集成

javascript - 如何更新嵌套的 mongo 对象

javascript - 为什么我的 Graphql 查询返回 null?

javascript - Promise.map 中的异步方法

node.js - 获取 Amazon Echo/Echo dot 的设备位置

excel - 注册 onSelectionChange

Vue.js 更改 {{ }} 标签