Skip to main content

Posts

Showing posts with the label Cross-domain

Allowing Cross domain POST request with JSON or XML content type in Java

Cross domain POST Request Cross domain POST requests supports only below content types and other content type are not allowed by default. application/x-www-form-urlencoded multipart/form-data text/plain So, if we try to make an ajax request to such POST URLs with XML or JSON content type, it will not be able to make request and give CORS error. Allowing Cross domain POST request with JSON or XML Content-Type We have our service implemented in Java and we will use Servlet Filter & request wrapper to build our solution. Please see my below POST to see how to implement CORS using java servlets which works well with GET methods or allowed content types with POST method. https://www.thetechnojournals.com/2020/03/cors-implementation-using-java-filter.html In this POST I will explain only additional things required for our problem with POST request, so I request you to go through the above link first. Request wrapper to change/ override the content-type to JSON or XML ...

CORS implementation using Java Filter

What is CORS CORS is cross origin resource sharing which allows or block the cross domain calls from a web application between different domains. By default you are able to make Ajax calls to other domain. To enable it that provider also need to add certain headers to allow the requester domain. CORS Headers Below are the required headers to implement the CORS. Access-Control-Allow-Origin In this header we specify the domain from which we want to allow the access, for example: example.com. Access-Control-Allow-Credentials This header specifies if it can pass authorization details in cross domain request. for example: true/false. Access-Control-Max-Age We can use this header to specify how long we want to cache the preflight request details like allowed methods, allowed headers etc. We can specify value in seconds and we can cache them for a long time as such details are not change frequently. Access-Control-Allow-Methods Here we specify the methods we want to allow for requ...