-
-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CORS issue on version 0.14.0 #1218
Comments
Why are you including |
this is my updated typescript snipped code const handleSubmit = async (event: SubmitEvent) => {
event.preventDefault();
console.log(JSON.stringify(form));
try {
const server_api_url = import.meta.env.VITE_API_SERVER_URL ?? 'http://localhost:5150/api';
const response = await fetch(`${server_api_url}/auth/login`, {
method: "POST", // HTTP method
headers: {
"Content-Type": "application/json", // Specify the data format
"Accept": "application/json"
},
body: JSON.stringify(form), // Send form data as JSON
});
console.log(response.status);
if (!response.ok) {
// biome-ignore lint/style/useTemplate: <explanation>
throw new Error("Failed to submit form: " + response.statusText);
}
const data = await response.json();
console.log("Success:", data);
// Optionally handle the response data, such as redirecting
// window.location.href = "/dashboard";
} catch (error) {
console.error("Error:", error);
}
}; this is the output from browsers console Access to fetch at 'http://localhost:5150/api/auth/login' from origin 'http://localhost:3000' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. this is the output from loco cli 2025-01-26T19:37:16.223001Z DEBUG http-request: tower_http::trace::on_request: started processing request http.method=OPTIONS http.uri=/api/auth/login http.version=HTTP/1.1 http.user_agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 environment=development request_id=2a3b2acf-d21f-489b-bf71-c490e08799cd
2025-01-26T19:37:16.223536Z DEBUG http-request: tower_http::trace::on_response: finished processing request latency=0 ms status=405 http.method=OPTIONS http.uri=/api/auth/login http.version=HTTP/1.1 http.user_agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 environment=development request_id=2a3b2acf-d21f-489b-bf71-c490e08799cd still the problem on OPTIONS (prelight) request. |
do you have example repo to share? |
this is the repo, thank you for taking look at it |
I do not see the frontend part. Can you add it? |
this is client app repo https://github.com/bendo01/client_app i've create login form on src/routes.index.tsx this is .env that i use
|
CORS determines whether resources can be accessed across different origins (e.g., different ports, domains, or protocols). You have two options to resolve this: Option 1: Set up a client proxy with ViteCreate a import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
proxy: {
"/api": {
target: "http://127.0.0.1:5150",
changeOrigin: true,
secure: false,
},
},
},
}) And update the following line in your code:
with this
Option 2: Enable middlewareIf you prefer not to use a proxy, you can enable middleware in your server. Add the following configuration:
Both solutions should work for your scenario. Choose the one that best fits your setup. |
implement your solution both on option 1 and option 2, your solutions work thank you for taking your time and give solution that works, i hope your solution benefit others who come with same problem best regards |
Description
i'm installing loco using loco cli generator but cors policy still block request from client app
To Reproduce
check using loco doctor
❯ cargo loco doctor
Finished
dev
profile [unoptimized + debuginfo] target(s) in 0.67sRunning
target/debug/locofreshinstall-cli doctor
2025-01-25T03:30:48.276412Z DEBUG app: loco_rs::bgworker: job queue ping requested environment=development
✅ SeaORM CLI is installed
✅ DB connection: success
✅ redis queue: queue connection: success
✅ Dependencies
✅ Loco version: latest
cargo toml
i'm using SolidStart template
here is my typescript snipped code
here is error message from browsers console
here is error from loco cli
Environment:
Operating System : OSX Sonoma 14.2.1 (23C71)
is anyone has this kind of problem using loco 0.14.0 ?
The text was updated successfully, but these errors were encountered: