While accessing the website url from Wats App message in mobile and laptop, I am getting below error.
Access to XMLHttpRequest at ‘https://www.my_domain.com.au/service/teamTokenGenerator’ from origin ‘https://my_domain.com.au’ 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.Understand this error TeamTokenGenerator.js:56
But accessing the site normally from browser www.my_domain.com.au, I don’t get any issue. Ít works fine, could someone please advise how can I fix the problem ?
//server.js
<code> const express = require('express');
const bodyParser = require("body-parser");
const cors = require('cors');
origin: ['https://www.my_domain.com.au'],
methods: 'GET,POST,PUT,DELETE',
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const sequelizeStatus = await sequelize.sync();
console.log("your server is up and running");
app.listen(port, () => console.log(`Listening on port ${port}`));
console.log(e, 'Database issue.');
<code> const express = require('express');
const bodyParser = require("body-parser");
const cors = require('cors');
const app = express();
app.use(cors({
origin: ['https://www.my_domain.com.au'],
methods: 'GET,POST,PUT,DELETE',
credentials: true
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
(async () => {
try {
const sequelizeStatus = await sequelize.sync();
console.log("your server is up and running");
app.listen(port, () => console.log(`Listening on port ${port}`));
} catch (e) {
console.log(e, 'Database issue.');
}
})();
</code>
const express = require('express');
const bodyParser = require("body-parser");
const cors = require('cors');
const app = express();
app.use(cors({
origin: ['https://www.my_domain.com.au'],
methods: 'GET,POST,PUT,DELETE',
credentials: true
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
(async () => {
try {
const sequelizeStatus = await sequelize.sync();
console.log("your server is up and running");
app.listen(port, () => console.log(`Listening on port ${port}`));
} catch (e) {
console.log(e, 'Database issue.');
}
})();
nginx configuration:
server_name my_domain.com.au www.my_domain.com.au;
return 301 https://$host$request_uri;
server_name my_domain.com.au www.my_domain.com.au;
ssl_certificate /var/www/ssl/certificate.crt;
ssl_certificate_key /var/www/ssl/my_domain.com.key;
##ssl_trusted_certificate /var/www/ssl/ca_bundle.crt
root /var/www/my_domain.com.au/build;
try_files $uri $uri/ /index.html;
##Optionally, handle specific requests such as favicon.ico
##This will handle static files under `/static/` directory
try_files $uri $uri/ =404;
alias /root/my_domain/public/images/;
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
add_header 'Access-Control-Allow-Origin' 'https://www.my_domain.com.au';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
<code>server {
listen 80;
server_name my_domain.com.au www.my_domain.com.au;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name my_domain.com.au www.my_domain.com.au;
ssl_certificate /var/www/ssl/certificate.crt;
ssl_certificate_key /var/www/ssl/my_domain.com.key;
##ssl_trusted_certificate /var/www/ssl/ca_bundle.crt
root /var/www/my_domain.com.au/build;
index index.html;
##Other SSL settings
location / {
try_files $uri $uri/ /index.html;
}
##Optionally, handle specific requests such as favicon.ico
location /favicon.ico {
try_files $uri =404;
}
##This will handle static files under `/static/` directory
location /static/ {
try_files $uri $uri/ =404;
}
location /images/ {
alias /root/my_domain/public/images/;
}
location /service {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
##CORS headers
add_header 'Access-Control-Allow-Origin' 'https://www.my_domain.com.au';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
}
}
</code>
server {
listen 80;
server_name my_domain.com.au www.my_domain.com.au;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name my_domain.com.au www.my_domain.com.au;
ssl_certificate /var/www/ssl/certificate.crt;
ssl_certificate_key /var/www/ssl/my_domain.com.key;
##ssl_trusted_certificate /var/www/ssl/ca_bundle.crt
root /var/www/my_domain.com.au/build;
index index.html;
##Other SSL settings
location / {
try_files $uri $uri/ /index.html;
}
##Optionally, handle specific requests such as favicon.ico
location /favicon.ico {
try_files $uri =404;
}
##This will handle static files under `/static/` directory
location /static/ {
try_files $uri $uri/ =404;
}
location /images/ {
alias /root/my_domain/public/images/;
}
location /service {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
##CORS headers
add_header 'Access-Control-Allow-Origin' 'https://www.my_domain.com.au';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
}
}
Client side code:
<code>const onSubmit = async (data) => {
const mobileNumber = data.mobileNumber;
setMobileNumber(mobileNumber);
const response = await axios.post(`${appUrl}/service/teamTokenGenerator`, { name, mobile: mobileNumber });
const generatedToken = response.data.token;
setToken(generatedToken);
setError("Failed to generate token. Please try again.");
REACT_APP_URL='https://www.my_domain.com.au'
"proxy": "https://www.my_domain.com.au", // in package.json
<code>const onSubmit = async (data) => {
const name = data.name;
const mobileNumber = data.mobileNumber;
setMobileNumber(mobileNumber);
try {
const response = await axios.post(`${appUrl}/service/teamTokenGenerator`, { name, mobile: mobileNumber });
const generatedToken = response.data.token;
setToken(generatedToken);
setTokenAvailable(true);
} catch (err) {
setError("Failed to generate token. Please try again.");
}
};
REACT_APP_URL='https://www.my_domain.com.au'
"proxy": "https://www.my_domain.com.au", // in package.json
</code>
const onSubmit = async (data) => {
const name = data.name;
const mobileNumber = data.mobileNumber;
setMobileNumber(mobileNumber);
try {
const response = await axios.post(`${appUrl}/service/teamTokenGenerator`, { name, mobile: mobileNumber });
const generatedToken = response.data.token;
setToken(generatedToken);
setTokenAvailable(true);
} catch (err) {
setError("Failed to generate token. Please try again.");
}
};
REACT_APP_URL='https://www.my_domain.com.au'
"proxy": "https://www.my_domain.com.au", // in package.json
Getting below error;
Access to XMLHttpRequest at ‘https://www.my_domain.com.au/service/teamTokenGenerator’ from origin ‘https://my_domain.com.au’ 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.Understand this error
TeamTokenGenerator.js:56