Hey I am not sure what else to do so I thought I would reach out here. I have two virtual hosts on a single droplet using DigitalOcean hosting services. The contact form isnt working so I went to check the browser console and these are the errors
Access to XMLHttpRequest at ‘https://api.website.ca/api/mail/send’ from origin ‘https://website.ca’ 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.
Error: Uncaught (in promise): kh: {“headers”:{“normalizedNames”:{},”lazyUpdate”:null,”headers”:{}},”status”:0,”statusText”:”Unknown Error”,”url”:”https://api.website.ca/api/mail/send”,”ok”:false,”name”:”HttpErrorResponse”,”message”:”Http failure response for https://api.website.ca/api/mail/send: 0 Unknown Error”,”error”:{“isTrusted”:true}}
So I have been trying to fix the error with Apache2 server conf by adding the following:
`<VirtualHost *:443>
` ServerAdmin [email protected]`
` ServerName api.website.ca`
` DocumentRoot /home/stephen/ftp/files/website/server`
` SSLEngine on`
` SSLCertificateFile /etc/letsencrypt/live/api.website.ca/fullchain.pem`
` SSLCertificateKeyFile /etc/letsencrypt/live/api.website.ca/privkey.pem`
` <Directory /home/user/ftp/files/website/server>
` Options Indexes FollowSymLinks`
` AllowOverride All`
`Require all granted`
# CORS settings
# <IfModule mod_headers.c>
# Header always set Access-Control-Allow-Origin “*”
# Header always set Access-Control-Allow-Methods “GET, POST, OPTIONS”
# Header always set Access-Control-Allow-Headers “Content-Type”
# Header always set Access-Control-Allow-Credentials “true”
#
`
` ErrorLog ${APACHE_LOG_DIR}/api.website.ca-error.log`
` CustomLog ${APACHE_LOG_DIR}/api.website.ca-access.log combined`
`</VirtualHost>`
This is not working – I have been testing with curl from my terminal and through the browser with the contact form. The CORS header does not show up:
curl -I -X OPTIONS https://api.kesil.ca/api/mail/send
HTTP/1.1 200 OK
Date: Sat, 17 Aug 2024 05:31:44 GMT
Server: Apache/2.4.58 (Ubuntu)
Allow: OPTIONS,HEAD,GET,POST
Content-Length: 0
I have also tried to use the .htaccess method and that was not successful either. I am fresh out of college and am working with my first client.
Let me know if I need to provide anything else thank you. It needs to be HTTPS.
Rio.Casanova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.