Mihai
Mihai

Reputation: 1

Cross-Origin (Camel-route and angularjs)

Hi i am trying to use apache camel to serve angularjs, but have the error: No 'Access-Control-Allow-Origin' header is present on the requested resource if i use restConfiguration. Something go wrong

In my blueprint i enabled CROS and set property headers like this, but don't work:

<restConfiguration bindingMode="json" component="jetty" enableCORS="true">
    <dataFormatProperty key="prettyPrint" value="true" />
    <corsHeaders key="Access-Control-Allow-Methods" value="GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH"></corsHeaders>
    <corsHeaders key="Access-Control-Allow-Origin" value="*"></corsHeaders>
    <corsHeaders key="Access-Control-Allow-Headers" value="Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"></corsHeaders>
    <corsHeaders key="Access-Control-Max-Age" value="3600"></corsHeaders>
</restConfiguration>

If i delete the rest configuration and set headers like below, i don't have errors.

 <route id="jetty" streamCache="true">
        <from uri="jetty:http://0.0.0.0:9100/ifom/miki?matchOnUriPrefix=true"/>
        <to uri="jetty:http://localhost:8080/om/miki?bridgeEndpoint=true&amp;throwExceptionOnFailure=false"/>
        <log message="jetty-done header: ${headers}"/>
        <setHeader headerName="Access-Control-Allow-Headers"><constant>X-Auth-Token, Content-Type</constant></setHeader>
        <setHeader headerName="Access-Control-Allow-Origin"><constant>*</constant></setHeader>
        <setHeader headerName="Access-Control-Allow-Methods"><constant>OPTIONS, POST</constant></setHeader>
    </route>

Why the restConfiguration don't work? Thank's

Upvotes: 0

Views: 1167

Answers (1)

user2192227
user2192227

Reputation:

I finally fixed this issue by setting the same value for Access-Control-Allow-Headers and Access-Control-Request-Headers on Camel and AngularJs.

AngularJs:

headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Request-Headers': 'access-control-allow-methods,access-control-allow-origin,authorization,content-type',
'Access-Control-Allow-Headers': 'access-control-allow-methods,access-control-allow-origin,authorization,content-type',
'Access-Control-Allow-Methods': 'GET, DELETE, POST, OPTIONS, PUT',
}

Camel:

.setHeader("Access-Control-Allow-Origin", constant("*"))        
.setHeader("Access-Control-Allow-Headers", constant("access-control-allow-methods,access-control-allow-origin,authorization,content-type"))        
.setHeader("Access-Control-Allow-Methods", constant("GET, DELETE, POST, OPTIONS, PUT"))

Upvotes: 0

Related Questions