• CORS Issue on REST WebService

  • Do you want to create a native client or integrate with third party applications: webservices are the solution.
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.
 #40588  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
 #40611  by jllort
 
Which language are you using for accessing webservices ? php ?
 #40640  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.
 #40658  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>
 #52354  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
Wen.xml
(19.15 KiB) Downloaded 413 times
 #52640  by jllort
 
Hi guilleinti are you getting an issue accessing from webservices?

About Us

OpenKM is part of the management software. A management software is a program that facilitates the accomplishment of administrative tasks. OpenKM is a document management system that allows you to manage business content and workflow in a more efficient way. Document managers guarantee data protection by establishing information security for business content.