Comments (4)
@fritx you are missing this from your example above:
ctx.set(
'Access-Control-Allow-Headers',
ctx.get('Access-Control-Request-Headers')
);
working code:
// Instead of using `@koa/cors` which conflicts with `koa-router`:
// https://github.com/evert0n/koa-cors/issues/30#issuecomment-301310688
routes.use(async (ctx, next) => {
ctx.set('Access-Control-Allow-Credentials', 'true');
ctx.set('Access-Control-Allow-Origin', '*');
await next();
});
routes.options('*', async (ctx, next) => {
ctx.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
ctx.set('Access-Control-Allow-Origin', '*');
ctx.set(
'Access-Control-Allow-Headers',
ctx.get('Access-Control-Request-Headers')
);
ctx.status = 204;
await next();
});
from koa-cors.
+1
This also makes sense if koa-router
is used with allowedMethods. This middleware sets the Allow:
headers in response to an OPTIONS
request. If koa-cors
runs first, the Allow:
headers from koa-router
are missing in the response.
from koa-cors.
+1 Related:
- "koa-router doesn't support Cross Domain request?" ZijianHe/koa-router#171
- "cannot get cross-site POST to work" axios/axios#191
I was having a problem with OPTIONS in koa-router, if a xhr library sends an OPTIONS request, the koa-cors wouldn't work.
I solved it by using the following code:
// drop koa-cors
// api cors
apiRouter.use(async (ctx, next) => {
ctx.set('Access-Control-Allow-Credentials', 'true')
ctx.set('Access-Control-Allow-Origin', '*')
await next()
})
// api options method
apiRouter.options('*', async (ctx, next) => {
ctx.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
ctx.set('Access-Control-Allow-Origin', '*')
ctx.status = 204
await next()
})
from koa-cors.
I had met this problem. And I solved it by setting options foo koa-cors, add PATCH
in methods:
const options = {
origin: true,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
};
app.use(cors(options))
from koa-cors.
Related Issues (20)
- errors when passing origin as option HOT 1
- Chrome content-type on POST HOT 5
- Cors headers on non OPTIONS HOT 2
- handling errors HOT 4
- 'settings' should be immutable HOT 1
- Add "Vary: Origin" to response when the server specifies an origin host rather than "*" HOT 2
- Accept a whitelist of origins? HOT 3
- expose PATCH by default HOT 7
- Canβt get CORS working with KOA
- Getting request origin within the origin function HOT 1
- overrides Access-Control-Allow-Methods
- with koa static page serving HOT 2
- Is it possible to declare multiple origins to allow? HOT 1
- Support koa@2 HOT 21
- methods option doesn't work in v2? HOT 1
- Support Typescript @types HOT 6
- Headers not sent on throw HOT 1
- Hi, Does this project is still maintained? HOT 2
- Should CORS be abandoned?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from koa-cors.