Comments (8)
Solved it! nginx
configuration issue.
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Headers *;
if ($request_method = OPTIONS) {
add_header Content-Type text/plain;
add_header Content-Length 0;
return 204;
}
default_type text/html;
content_by_lua_block {
require("lapis").serve("app")
}
}
And no-cors
on requests in the frontend for dev builds.
from lapis.
CORS issues generally happen when the domain your JavaScript is running on is different than a domain you are trying to request. Can you share these details?
from lapis.
Cheers:
Lapis
$ lapis server
...
[500] OPTIONS /sign-in - {}, client: 127.0.0.1, server: , request: "OPTIONS /sign-in HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:3000/"
JS client, running dev server (vite) on the same machine
const res = await fetch('http://localhost:8080/sign-in', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(fields()),
})
from lapis.
I can see a 500 response code in the log line you shared for lapis. That would be a server error, so you may want to look at content of the request in your browser, or execute it with CURL to see what the error is. If you're using something like respond_to
in lapis, then keep in mind you have to explicitly implement an OPTIONS
handler
from lapis.
That's the thing, all endpoints work fine against the browser/curl, including things like sessions, getting data from the DB etc. The API isn't broken, only blocking CORS when the browser is making request from a localhost
frontend.
For example, I can hit a protected endpoint and get 403, sign in 200, and hit that same endpoint again for 200. If I try to do the same thing from the frontend, I get these errors.
from lapis.
That's the thing, all endpoints work fine against the browser/curl, including things like sessions, getting data from the DB etc. The API isn't broken, only blocking CORS when the browser is making request from a localhost frontend.
The logs you shared showed a request that made to your Lapis app returning a 500 status code, aka server error (this would be an uncaught Lua error unless you explicitly returned a 500 status yourself):
[500] OPTIONS /sign-in - {}, client: 127.0.0.1,
The example you gave where you suggested it was not broken would not trigger an OPTIONS, since that is a secondary request created only by browsers during a cross domain request. Hence my suggestion that you likely did not implement an OPTIONS request handler in your Lapis application. I recommend understanding how to read the result of a 500 error as it will help you out with debugging things in the future. Hope that helps
from lapis.
Solved it!
nginx
configuration issue.location / { add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods *; add_header Access-Control-Allow-Headers *; if ($request_method = OPTIONS) { add_header Content-Type text/plain; add_header Content-Length 0; return 204; } default_type text/html; content_by_lua_block { require("lapis").serve("app") } }
And
no-cors
on requests in the frontend for dev builds.
I'm having problems with cors as well, tried this but unfortunately it doesn't work for me, any thoughts?
from lapis.
the solution was adding the add_header Access ....
on the options response
if ($request_method = OPTIONS) {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers *;
add_header Access-Control-Allow-Methods *;
add_header Content-Type application/json;
add_header Content-Length 0;
return 204;
}
from lapis.
Related Issues (20)
- route name cannot be one of the paths HOT 2
- Specifying backend=pgmoon now required? HOT 1
- Value not available in sub-sub-template if render(..., {value = ...}) used in the template
- Default Route - Open Redirect HOT 1
- Module Migrations not found HOT 1
- Error when trying to start server HOT 3
- display "listening on ..." on startup HOT 2
- Loading static files like css with cqueues server HOT 2
- Database type could not be determined from configuration (postgres, mysql, sqlite)
- How to get client ip? HOT 1
- Remove luarocks dependency
- request:read_body_as_string() fails if body was buffered to a temporary file with nginx
- SQL fields all nil using sqlite
- app:before_filter(json_params) not work? HOT 1
- Feature Request: Foreign Key constraint option in schema types
- Add database field types in docs
- Lua 5.1 Support HOT 2
- Parameter parser doesn't parse `+` correctly. HOT 2
- Database request error : "Attempt to yield across C-call boundary"
- cmd
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 lapis.