'HTTPS Request : CORS error but I still get data
I'm making HTTPS request to a private API (hosted Itop), I get an response but I get CORS Multiple Origin Not Allow
error so my JavaScript program can't use the response content.
I'm supposed to have CORS authorization
The requests are POST
, made with fetch, there isn't preflight (OPTIONS
) request made before (fetch did them alone for my other GET
API request but didn't here)
Also some server response time is long for firefox (~2s) but it don't seems to change anything
Solution 1:[1]
It's not allowed to send multiple Access-Control-Allow-Origin
headers or multiple origins in one header in the same response. In the comments, you described two same Access-Control-Allow-Origin
headers in one response. Even two same origins aren't allowed. Remove one header in the backend code.
Solution 2:[2]
firefox is the best browser to identify this issue, you can fix this issue by following this path. when you sending a request from frontend,and then response will come from backend. but browser is not allowed to aceess to javascript. this is the cors error.
1.open ur backend, then create a package call CorsFilter. 2. and then create a filter servlet 3. paste this code
@WebFilter(filterName = "CorsFilter", urlPatterns = "/*")
public class CorsFilter extends HttpFilter {
@Override
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
String origin = req.getHeader("Origin");
if (origin != null && origin.toLowerCase().contains(getServletContext().getInitParameter("origin"))) {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
res.setHeader("Access-Control-Expose-Headers", "Content-Type");
if (req.getMethod().equals("OPTIONS")) {
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET, PUT, POST, DELETE, HEAD");
}
}
chain.doFilter(req, res);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jabaa |
Solution 2 |