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 »

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: 11518
Joined: Fri Dec 21, 2007 11:23 am
Location: Sineu - ( Illes Balears ) - Spain
Contact:

Re: CORS Issue on REST WebService

Post by jllort »

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 »

I am using Angular JS and also tried simple Ajax for accessing rest webservice
jllort
Moderator
Moderator
Posts: 11518
Joined: Fri Dec 21, 2007 11:23 am
Location: Sineu - ( Illes Balears ) - Spain
Contact:

Re: CORS Issue on REST WebService

Post by jllort »

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 »

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: 3113
Joined: Tue Dec 11, 2007 6:02 pm
Location: Alicante, Spain
Contact:

Re: CORS Issue on REST WebService

Post by pavila »

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 »

Nope i didnt create it

Sorry for replying late.
lalitpareshan
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: Sun Nov 08, 2020 10:34 am

Re: CORS Issue on REST WebService

Post by lalitpareshan »

Followed the steps but still failing , if you can help

Error

Code: Select all

project_view.html:1 Access to XMLHttpRequest at 'http://okmAdmin:admin@139.168.136.192:8080/OpenKM/services/rest/document/getContent?docId=93cbc827-66b0-4ef6-b2c2-f3261c583004' from origin 'http://139.168.136.192:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
app.js:1599 
okmAdmin:admin@139.168.136.192:8080/OpenKM/services/rest/document/getContent?docId=93cbc827-66b0-4ef6-b2c2-f3261c583004:1 Failed to load resource: net::ERR_FAILED
DevTools failed to load SourceMap: Could not load content for http://139.168.136.192:5000/web/assets/js/popper.min.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
DevTools failed to load SourceMap: Could not load content for http://139.168.136.192:5000/web/assets/js/bootstrap.min.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
DevTools failed to load SourceMap: Could not load content for http://139.168.136.192:5000/web/assets/css/bootstrap.min.css.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
Attachments
web.zip
Wen.xml
(19.15 KiB) Downloaded 25 times
jllort
Moderator
Moderator
Posts: 11518
Joined: Fri Dec 21, 2007 11:23 am
Location: Sineu - ( Illes Balears ) - Spain
Contact:

Re: CORS Issue on REST WebService

Post by jllort »

I do not understanding why are you sharing the web.xml file?
Can you try using one of the SDK's ( JAVA, NET or PHP )? -> here you'll find information about them https://docs.openkm.com/kcenter/
guilleinti
Fresh Boarder
Fresh Boarder
Posts: 1
Joined: Fri Jul 16, 2021 3:05 pm
Contact:

Re: CORS Issue on REST WebService

Post by guilleinti »

I really dont understand what's causing the error :?
jllort
Moderator
Moderator
Posts: 11518
Joined: Fri Dec 21, 2007 11:23 am
Location: Sineu - ( Illes Balears ) - Spain
Contact:

Re: CORS Issue on REST WebService

Post by jllort »

Hi guilleinti are you getting an issue accessing from webservices?
Post Reply