2

error:
Unhandled promise rejection Error: "Network Error"

my front-end vue application is running in http://localhost:8080 and backend is running in in http://localhost:8082 I'm requesting get request from axios from vue file to the backend.

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);
        });

and here is my endpoint

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();
});
  });})

what could be the reason for this error!?

Kishore S
  • 163
  • 3
  • 10
  • Possible duplicate of [Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote ...............CORS header ‘Access-Control-Allow-Origin’ missing](https://stackoverflow.com/questions/45611400/cross-origin-request-blocked-the-same-origin-policy-disallows-reading-the-remot) – IftekharDani Nov 06 '18 at 10:28
  • yes!..that resolved one error!, now there's one more – Kishore S Nov 06 '18 at 11:01
  • Please make sue `http://192.168.43.35:8082` is started? – IftekharDani Nov 06 '18 at 11:07
  • yes! I can see the response by typing out in the URL field of the browser – Kishore S Nov 06 '18 at 11:10

1 Answers1

0

Did you add origin headers to your middleware on server-side?

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();
});
ItsMyLife
  • 458
  • 7
  • 21