#cors error

1 messages · Page 1 of 1 (latest)

sacred swift
#

I have "CORS request did not succeed error" in my browser. My backend is written using Spring Boot and frontend is written in Svelte (JS)

My CORS Filter:

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        chain.doFilter(req, res);
    }
analog hatchBOT
#

<@&1004656351647117403> please have a look, thanks.

analog hatchBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

The "CORS request did not succeed" error occurs when the browser blocks a cross-origin request due to the same-origin policy. This policy restricts web pages from making requests to a different domain than the one that served the page.

To fix this issue, you need to configure CORS (Cross-Origin Resource Sharing) in your Spring Boot backend. Here's how you can do it:

  1. Add the @CrossOrigin annotation to your controller class or specific methods that need CORS support. For example:
#
@RestController
@CrossOrigin(origins = "http://localhost:5000")
public class MyController {
    // Controller methods...
}
#

This allows requests from http://localhost:5000 to access your backend.

  1. If you want to allow requests from any origin, you can use the wildcard *:
#
@CrossOrigin(origins = "*")
#

Note that using * allows any origin, which may not be suitable for production environments due to security concerns.

  1. If you need more fine-grained control over CORS settings, you can create a custom CORS filter by implementing the javax.servlet.Filter interface. Here's an example:
#
@Component
public class CustomCorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        HttpServletRequest request = (HttpServletRequest) req;

        response.setHeader("Access-Control-Allow-Origin", "http://localhost:5000");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
        response.setHeader("Access-Control-Max-Age", "3600");

        chain.doFilter(req, res);
    }

    // Other methods...
}
#

In this example, the filter allows requests from http://localhost:5000 and specifies the allowed methods, headers, and maximum age.

Make sure to register the custom CORS filter in your Spring Boot application. You can do this by adding the @ComponentScan annotation to your main application class or by registering it manually in your configuration.

With these changes, your backend should allow cross-origin requests from your Svelte frontend without triggering the CORS error.

still cloak
#

Can you show the error(s) you have please?

#

(when you make the api call(s))

sacred swift
#

!close

analog hatchBOT
# sacred swift !close

Looks like you attempted to use a command? Please note that we only use slash-commands on this server 🙂

Try starting your message with a forward-slash / and Discord should open a popup showing you all available commands.
A command might then look like /foo 👍

#

Closed the thread.

still cloak
sacred swift
#

instead of this filter

still cloak
#

🤔

#

Hmm why?

sacred swift
#

but it works so im happy xdd

still cloak
#

Strange issue

#

Does your system has authentication process?

#

I think it should be

sacred swift
still cloak
#

There is an http method thay may cause the issue i think it's called OPTIONS

sacred swift
still cloak
#

If you allow OPTIONS

#

I think it solves cors issues

#

Anyway

sacred swift
#

in my filter

#

before

#

but it didnt work either

still cloak
#

Ah

#

Anyway congratulations

sacred swift