Docxtemplater CORS policy on webserver

264 Views Asked by At

I am trying to build a little website where i need to use Docxtemplater. (https://docxtemplater.com/)

This is a node module used to edit word documents (.docx). In the docs (https://docxtemplater.com/docs/generate/), it is stated "Please note that if you want to load a docx from your filesystem, you will need a webserver or you will be blocked by CORS policy."

I have tried uploading my website to a host (https://www.000webhost.com/), but i get the error:

mathiastester.000webhostapp.com/:1 Access to XMLHttpRequest at 'https://docxtemplater.com/tag-example.docx' from origin 'https://mathiastester.000webhostapp.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Does anyone have any clue on what is going on...?

Here is the website: https://mathiastester.000webhostapp.com/

1

There are 1 best solutions below

5
On

Just like the example in the link you provided, your AJAX code needs to make a request to some server-side code running on your webserver, which then sends the request to docxtemplater, because docxtemplater doesn't allow cross-origin ("CORS") requests to it.

Simply put, a CORS request is an AJAX request where the request is sent to a different "origin" (combination of domain, port and protocol) than the one it is running from. These are disallowed by browsers by default for security reasons, but the remote server can, if it wishes, return response headers indicating to the browser that the request can in fact be allowed. docxtemplater doesn't do that, and the documentation you quoted is telling you not to make a CORS request to their servers. However judging by the error message you've shown us, you've done exactly that in your code.

See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS for more info on what CORS is.