CORS Issue on REST WebService

Do you want to create a native client or integrate with third party applications: webservices are the solution.
Forum rules
Please, before asking something see the documentation wiki or use the search feature of the forum. And remember we don't have a crystal ball or mental readers, so if you post about an issue tell us which OpenKM are you using and also the browser and operating system version. For more info read How to Report Bugs Effectively.
Post Reply
Jahangir
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: Tue Sep 22, 2015 12:37 pm

CORS Issue on REST WebService

Post by Jahangir » Tue Sep 22, 2015 12:51 pm

Hi,
I am facing CORS issue when trying to access REST WS of openKM from another application hosted on different port.

Code: Select all

$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode('okmAdmin' + ':' + 'admin');
$http.get('http://localhost:8081/OpenKM/services/rest/folder/getChildren?fldId=8c910ef3-2075-49ba-9e83-7237d4ee45fd').then(
  function(value) {
     alert('success value' + value);
  }, function(reason) {
    alert('failure value'+reason);	
  }, function(value) {
  }
ERROR:

Code: Select all

XMLHttpRequest cannot load http://localhost:8081/OpenKM/services/rest/folder/getChildren?fldId=8c910ef3-2075-49ba-9e83-7237d4ee45fd. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401.
I have added CORS Filter to Web.xml of Tomcat but its still not working.

I can access simple file hosted on http://localhost:8081/xyz.xml but unable to access openKM REST service

jllort
Moderator
Moderator
Posts: 9119
Joined: Fri Dec 21, 2007 11:23 am
Location: Sineu - ( Illes Balears ) - Spain
Contact:

Re: CORS Issue on REST WebService

Post by jllort » Thu Sep 24, 2015 9:32 am

Which language are you using for accessing webservices ? php ?

Jahangir
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: Tue Sep 22, 2015 12:37 pm

Re: CORS Issue on REST WebService

Post by Jahangir » Mon Sep 28, 2015 7:41 am

I am using Angular JS and also tried simple Ajax for accessing rest webservice

jllort
Moderator
Moderator
Posts: 9119
Joined: Fri Dec 21, 2007 11:23 am
Location: Sineu - ( Illes Balears ) - Spain
Contact:

Re: CORS Issue on REST WebService

Post by jllort » Wed Sep 30, 2015 10:09 am

I do not success accessing webservices across javascript, some months ago I tryed several strategies and I do not success with no one.

Jahangir
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: Tue Sep 22, 2015 12:37 pm

Re: CORS Issue on REST WebService

Post by Jahangir » Fri Oct 02, 2015 1:12 pm

I finally managed to run $ajax.get script from different domain with CORS settings by adding apache CORS filter to Web.xml. The only important point here is that you need to add these lines just above springSecurityChainFilter and it will work fine.
you can also change * with specific domain for more security.

Code: Select all

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>  
  <init-param>
    <param-name>cors.allowed.origins</param-name>
    <param-value>*</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.methods</param-name>
    <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.headers</param-name>
    <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization</param-value>
  </init-param>     
</filter>

<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

pavila
Moderator
Moderator
Posts: 3001
Joined: Tue Dec 11, 2007 6:02 pm
Location: Alicante, Spain
Contact:

Re: CORS Issue on REST WebService

Post by pavila » Tue Nov 17, 2015 12:31 pm

Have you created this JIRA http://jira.openkm.com/browse/OCM-6 ?

Jahangir
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: Tue Sep 22, 2015 12:37 pm

Re: CORS Issue on REST WebService

Post by Jahangir » Thu Dec 17, 2015 5:19 am

Nope i didnt create it

Sorry for replying late.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest