-1

Im trying to get a text/html of an url differnt from origin, but i cant get a POST because a CORS block.

Im makeing the requisition in angular this way:

const express = require('express');
const cors = require('cors');
validateOperador(login,pass){
    const url = `${this.operadoresValidateUrl}a=${login}&b=${pass}`
    return   this.http.get(url,{headers: 
    {'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*' } })
  }

and my node backend using express, is configured this way:

    app.use(function (req, res, next) {
      //Enabling CORS
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
      res.header("Access-Control-Allow-Headers", 
      "Origin, X-Requested-With, Content-Type, Accept, x-client-key, x-client-token, x-client-secret, Authorization");
        next();
      });

    httpServer = http.createServer(app);
    app.use(cors())
    app.use(bodyParser.json())
    app.use(compression());

Im getting this error:

Access to XMLHttpRequest at 'my api url' from origin 'my project url' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

This is not a duplicated question, because I´ve already installed and runned CORS

  • Its not the same – Gustavo Kotarsky Aug 12 '19 at 19:46
  • Refer to https://expressjs.com/en/resources/middleware/cors.html for examples – Jota.Toledo Aug 12 '19 at 19:55
  • Im not able to put this examples in my project because what i need is to get an html of a url different from origin – Gustavo Kotarsky Aug 12 '19 at 20:07
  • If you are in control of the backend to which your frontend is making a request, then you are able to configure CORS correctly in the backend. If this is not the case, feel free to update your question with more details. _This is not a duplicated question, because I´ve already installed and runned CORS_ what did you exactly do? Update your question with the relevant code – Jota.Toledo Aug 13 '19 at 06:03
  • Question updated – Gustavo Kotarsky Aug 13 '19 at 19:11
  • It's a clear dup. You need to pass a config object to the cors call, and then enable cors in preflight requests as explained in the docs. – Jota.Toledo Aug 14 '19 at 06:34

2 Answers2

0

You must not add 'Access-Control-Allow-Origin': '*' from client side.

Also you are doing a GET and not a POST not sure if that's what you want.

Also it seems a duplicate of this

-1

Install cors: npm install cors

const cors = require('cors');

Then use in your index.js

app.use(cors())

Daniel Manfred
  • 335
  • 2
  • 5