Angular $ http.delete CORS error (request before flight)

I have an Angular (v1.13.15) application and Express.js (v4.12.4) as a backend.

I have a DELETE method in my backend and I have enabled CORS support.

But when I use Angular $ http.delete, I encounter this error

No header "Access-Control-Allow-Origin" is present on the requested resource.

I tried using jQuery, $ .ajax () for this, and I get the same problem!

I also tried using POSTMAN to execute a DELETE request, and there is no problem.

But I have no problem using my Angular for my GET and POST method.

Can I find out what the problem is?

My backend address is http: // localhost: 3000

I serve my AngularJS with gulp -webserver http: // localhost: 8000

My server code

exports.deleteGPSData = (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

let id = req.params.id;

res.send('here');
}

Angular

$http.delete(API_URL + '/gps/' + id)
                .success(function(res) {
                    if (res.result !== 1) {
                        return defer.reject(new Error(id + ' failed to delete'));
                    }

                    defer.resolve(res.id);
                })
                .error(function(status) {
                    defer.reject(status);
                });

GET POST! DELETE, CORS!

Google Chrome

Angular CORs problme

Postman,

Postman delete

+3
4

,

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

CORS DELETE OPTIONS, DELETE

, , OPTIONS, next(), DELETE

:

app.options('/gps/:id', routes.gps.optionGPSData);
app.delete('/gps/:id', routes.gps.deleteGPSData);

exports.optionGPSData = (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type');

next();
}

POSTMAN DELETE? ( , HTTP- DELETE , OPTIONS), - OPTIONS ( )

* shoutout @FrankerZ, POSTMAN Chrome, , , cors (https://www.npmjs.com/package/cors), , - !

+6
0
app.options('/gps/:id', function(req, res) { res.json({}) });
app.delete('/gps/:id', routes.gps.optionGPSData);

( ).

0

, . , .

CORS , .

server.js( )

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Request-Headers", "*");
  res.header('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,X-HTTP-Method-Override, Content-Type, Accept, Authorization");
  res.header("Access-Control-Allow-Credentials", "true");
  next();
});

, CORS.

, API- ( java-)

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Request-Headers", "*");
  res.header('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,X-HTTP-Method-Override, Content-Type, Accept, Authorization");
  res.header("Access-Control-Allow-Credentials", "true");
  next();
});

This problem before the flight only occurs when we try to connect to the local host from different sources.

0
source

All Articles