0

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/

Dharman
  • 30,962
  • 25
  • 85
  • 135
MATTI
  • 9
  • 5
  • Just to get back to you, I'm the creator of docxtemplater. I've just fixed the issue, ie, the code that you copied from the docs should work now, even when using my server. That was what was intended but I think I broke this a few weeks ago without noticing. – edi9999 Oct 21 '21 at 18:59
  • Oh cool! I somehow also got it to work on the 15th, idk how. Thanks for the input! – MATTI Oct 26 '21 at 08:25

1 Answers1

1

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.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Thanks for the help! I will try and read a bit about it! But tbh do not have any clue what most of this means, i just wanna edit my word docx through js..... – MATTI Oct 14 '21 at 07:25
  • Well you may "just" want to do that, but you simply can't call docxtemplater direct from JS - they don't allow it (probably for security reasons). So as my answer explains, the way you have to do it is to make your JS call your server, and then have your server call docxtemplater. – ADyson Oct 14 '21 at 08:05
  • I am sorry for my confusment, but how exactly would i go about calling my server, and returning the needed info? I am not very familiar with all of this calling to server thing. Thanks in advance. – MATTI Oct 14 '21 at 10:09
  • You just make an HTTP AJAX request to your server instead of direct to docxtemplater. Then you have code on your server makes a HTTP request to docxtemplater, gets the results and outputs it back to your JS (as the response to the AJAX call) – ADyson Oct 14 '21 at 10:12
  • Alright thanks for that! I will try and work with that. – MATTI Oct 14 '21 at 10:13