1const cors = require('cors');
2const corsOptions ={
3 origin:'http://localhost:3000',
4 credentials:true, //access-control-allow-credentials:true
5 optionSuccessStatus:200
6}
7app.use(cors(corsOptions));
1/*
2npm i cors
3or
4 yarn add cors
5then in your node app
6*/
7const cors = require('cors');
8const corsOptions ={
9 origin:'http://localhost:3000',
10 credentials:true, //access-control-allow-credentials:true
11 optionSuccessStatus:200
12}
13app.use(cors(corsOptions));
1//Access to XMLHttpRequest at 'http://localhost/[api path].php' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
2
3//The error is simply saying that "Content-Type" is missing from "Access-Control-Allow-Headers".
4
5//Therefore we need to add "Content-Type" to "Access-Control-Allow-Headers".
6
7<?php
8header('Access-Control-Allow-Headers: Content-Type');
9-----
10?>
1@app.route('your route', methods=['GET'])
2def yourMethod(params):
3 response = flask.jsonify({'some': 'data'})
4 response.headers.add('Access-Control-Allow-Origin', '*')
5 return response
1app.use(function (request, response, next) {
2 response.header("Access-Control-Allow-Origin", "*");
3 response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
4 next();
5});
1app.use(function (req, res, next) {
2 //Enabling CORS
3 res.header("Access-Control-Allow-Origin", "*");
4 res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
5 res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,
6 Accept, x-client-key, x-client-token, x-client-secret, Authorization");
7 next();
8 });