Changed the code to:
(allowOrigins with * or with http://localhost:56696
@Path("/office/")
public class OfficeOffice {
@GET
@CrossOriginResourceSharing(allowAllOrigins = true, allowOrigins = "*",
allowCredentials = true)
@Path("/offices/{id}/")
@Produces("text/plain")
public String getCustomer(@PathParam("id") String id) {
return "Systems";
}
}
Tested for Chrome and IE.
In IE it works. In Chrome request is reaching service. and response is
returned but in browser shows same error message
XMLHttpRequest cannot load http://localhost:9001/office/offices/123.
Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:56696' is therefore not allowed access.
On Fri, Dec 4, 2015 at 4:48 PM, Sergey Beryozkin <[email protected]>
wrote:
> Perhaps allowOrigins should be set to a wildcard ?
>
>
> On 04/12/15 11:09, Shashank Dutt Jha wrote:
>
>> still same error
>> Error:
>> XMLHttpRequest cannot load http://localhost:9001/office/offices/123. No
>> 'Access-Control-Allow-Origin' header is present on the requested resource.
>> Origin 'http://localhost:56696' is therefore not allowed access.
>>
>>
>>
>> Service code:
>>
>> @Path("/office/")
>> public class OfficeOffice {
>>
>>
>> @GET
>> @CrossOriginResourceSharing(allowAllOrigins = true, allowOrigins = "
>> http://localhost:56696" ,allowCredentials = true)
>> @Path("/offices/{id}/")
>> @Produces("text/plain")
>> public String getCustomer(@PathParam("id") String id) {
>> System.out.println("Serving request to customer id\t"+id);
>> return "Systems";
>> }
>> }
>>
>> On Fri, Dec 4, 2015 at 4:21 PM, Shashank Dutt Jha <[email protected]>
>> wrote:
>>
>>
>>> @CrossOriginResourceSharing(
>>> allowOrigins = {
>>> "http://localhost:56696", "*"
>>> },
>>> allowCredentials = true,
>>> maxAge = 1,
>>> allowHeaders = {
>>> "X-custom-1", "X-custom-2"
>>> },
>>> exposeHeaders = {
>>> "X-custom-3", "X-custom-4"
>>> }
>>> )
>>>
>>> @Path("/office/")
>>> public class OfficeOffice {
>>>
>>>
>>> @GET
>>> @Path("/offices/{id}/")
>>> @Produces("text/plain")
>>> public String getCustomer(@PathParam("id") String id) {
>>> System.out.println("Serving request to customer id\t"+id);
>>> return "Systems";
>>> }
>>> }
>>>
>>> On Fri, Dec 4, 2015 at 10:00 AM, Shashank Dutt Jha <
>>> [email protected]>
>>> wrote:
>>>
>>> I added following code: Still facing issue
>>>>
>>>> @CrossOriginResourceSharing(
>>>> allowOrigins = {
>>>> "http://localhost:56696"
>>>> },
>>>> allowCredentials = true,
>>>> maxAge = 1,
>>>> allowHeaders = {
>>>> "X-custom-1", "X-custom-2"
>>>> },
>>>> exposeHeaders = {
>>>> "X-custom-3", "X-custom-4"
>>>> }
>>>> )
>>>>
>>>> @Path("/office/")
>>>> public class OfficeOffice {
>>>> int count = 0;
>>>> Object obj = new Object();
>>>> @Context
>>>> private HttpHeaders headers;
>>>>
>>>> @GET
>>>> @CrossOriginResourceSharing(allowOrigins = { "http://localhost:56696"
>>>> },
>>>> allowCredentials = false, exposeHeaders = {
>>>> "X-custom-3", "X-custom-4" })
>>>> @Path("/offices/{id}/")
>>>> @Produces("text/plain")
>>>> public String getCustomer(@PathParam("id") String id) {
>>>> synchronized (obj) {
>>>> ++count;
>>>> System.out.println("----invoking getCustomer, Customer id is: " +
>>>> count);
>>>> }
>>>> return "Systems";
>>>> // return Response.status(Status.OK).entity("works").build();
>>>> }
>>>>
>>>> // This method will do a preflight check itself
>>>> @OPTIONS
>>>> @Path("/")
>>>> @LocalPreflight
>>>> public Response options() {
>>>> String origin = headers.getRequestHeader("Origin").get(0);
>>>> if ("http://localhost:56696".equals(origin)) {
>>>> return Response.ok()
>>>>
>>>> .header(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS, "DELETE PUT")
>>>>
>>>> .header(CorsHeaderConstants.HEADER_AC_ALLOW_CREDENTIALS, "false")
>>>>
>>>> .header(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN, "
>>>> http://localhost:56696")
>>>> .build();
>>>> } else {
>>>> return Response.ok().build();
>>>> }
>>>> }
>>>>
>>>> @GET
>>>> @CrossOriginResourceSharing(
>>>> allowOrigins = { "http://localhost:56696" },
>>>> allowCredentials = true,
>>>> exposeHeaders = { "X-custom-3", "X-custom-4" }
>>>> )
>>>> @Produces("text/plain")
>>>> @Path("/annotatedGet/{echo}")
>>>> public String annotatedGet(@PathParam("echo") String echo) {
>>>> return echo;
>>>> }
>>>>
>>>> On Thu, Dec 3, 2015 at 4:51 PM, Sergey Beryozkin <[email protected]>
>>>> wrote:
>>>>
>>>> Please do not copy exception traces into Subject :-)
>>>>>
>>>>> Have a look at
>>>>> http://cxf.apache.org/docs/jax-rs-cors.html
>>>>>
>>>>> Register that filter as a provider
>>>>>
>>>>> Sergey
>>>>> On 03/12/15 11:04, Shashank Dutt Jha wrote:
>>>>>
>>>>> I want the REST server to be accessible to web/ browser app. Which is
>>>>>> correct example to refer to.
>>>>>>
>>>>>> On Wed, Dec 2, 2015 at 11:15 AM, Shashank Dutt Jha <
>>>>>> [email protected]>
>>>>>> wrote:
>>>>>>
>>>>>> Server side code:
>>>>>>
>>>>>>>
>>>>>>> protected Server() throws Exception {
>>>>>>> JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
>>>>>>> sf.setResourceClasses(CustomerService.class);
>>>>>>> sf.setResourceProvider(CustomerService.class,
>>>>>>> new SingletonResourceProvider(new CustomerService()));
>>>>>>> sf.setAddress("http://161.85.91.7:9000/");
>>>>>>> sf.create();
>>>>>>>
>>>>>>>
>>>>>>> -------
>>>>>>> When I try to connect to server from chrome browser I get following
>>>>>>> error
>>>>>>>
>>>>>>> MLHttpRequest cannot load http://161.85.91.7:9001/office/offices/123
>>>>>>> .
>>>>>>> Response to preflight request doesn't pass access control check: No
>>>>>>> 'Access-Control-Allow-Origin' header is present on the requested
>>>>>>> resource.
>>>>>>> Origin 'http://localhost:56696' is therefore not allowed access.
>>>>>>>
>>>>>>>
>>>>>>> How to resolve this issue?
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>> --
>>>>> Sergey Beryozkin
>>>>>
>>>>> Talend Community Coders
>>>>> http://coders.talend.com/
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>
> --
> Sergey Beryozkin
>
> Talend Community Coders
> http://coders.talend.com/
>