webdav-client: CORS-header ‘Access-Control-Allow-Origin’ missing (Nextcloud)
Just tried to follow the guide in the readme.md to connect to a nextcloud server. The code is
var client = Webdav(
"https://some.nextcloudserver.nl/remote.php/webdav",
"username"
);
client
.getDirectoryContents("/", { withCredentials: true })
.then(function(contents) {
console.log(JSON.stringify(contents, undefined, 4));
});
But when it executes the console reports ‘CORS-header ‘Access-Control-Allow-Origin’ missing’.
More information: using a standard empty VueJS project template with webdav version 1.6.1, on http://localhost:8080 and the nextcloud server is a remote server. Not sure if this is expected behaviour or not, since the example in the readme.md is doing exactly the same.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 19 (9 by maintainers)
So I actually managed to get this stuff working.
When setting up the whole stuff it is very important to configure the .htaccess file of the hosting. You will see a line with the following text: “#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####”. All the changes below are the changes I made above that line. The main thing you have to do is edit: <IfModule mod_env.c>
As you see here you can replace the part with your own website (and add additional websites by using | as a seperator):
SetEnvIf Origin "http(s)?://(www.mywebsite.com|localhost:8080)$" AccessControlAllowOrigin=$0I have added localhost:8080 as I am working with Vue on my website. This is highly discouraged and should be removed once the website is deployed.
Now follow the tutorial as given on the README:
In case you are getting a PROPFIND 405 error, this is most likely the solution:
.getDirectoryContents("remote.php/dav/files/...")instead of.getDirectoryContents("/")It could be that I forgot a few changes in the .htaccess file, so in case it does not work out and you are still getting CORS errors just ping me.
This should have nothing to do with CORS, as these paths are appended to the host passed in during the factory creation… The two examples you gave are not equivalent. The host should include the
remote.php/dav/files/in your example. So if your domain isexample.com, your full connection URL should behttps://example.com/remote.php/dav/files/.