@whireless

Почему запрос из постороннего источника заблокирован?

Всем привет, пожалуйста подскажите, в чем проблема? Консоль выдаёт такое -

Запрос из постороннего источника заблокирован: Политика одного источника запрещает чтение удаленного ресурса на https://example.com:8443/server. (Причина: не удалось выполнить запрос CORS). Код состояния: (null).

Вот запрос:
....
const sendData = async () => {
const response = await fetch(
              'https://example.com:8443/server',
              {
                method: 'POST',
                body: new URLSearchParams(new FormData(payForm)),
              }
            );
const data = await response.text();
};


Вот код сервера:
// Сервер

const https = require('node:https');
const fs = require('node:fs');
const app = require('express')();
const bodyParser = require('body-parser');

const host = 'example.com';
const port = 8443;

const options = {
  cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem'),
  key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
};

app.disable('x-powered-by');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});

https.createServer(
  options,
  app,
).listen(port, host, () => {
  console.log(`Привет от сервера! https://${host}:${port}`);
});


Вот Nginx:
http {
	sendfile on;
	tcp_nopush on;
	types_hash_max_size 2048;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	# SSL Settings
	include /etc/letsencrypt/options-ssl-nginx.conf;

  # HTTP-заголовки
	add_header X-Frame-Options           "SAMEORIGIN" always;
	add_header X-XSS-Protection          "1; mode=block" always;
	add_header X-Content-Type-Options    "nosniff" always;
	add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

	server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
	}

	server {
    listen 443 ssl;
    listen [::]:443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
      root /var/www/example.com/;
      index index.html;
    }

    location /server/ {
      proxy_pass https://0.00.00.000:8443;
    }
	}
  • Вопрос задан
  • 227 просмотров
Решения вопроса 1
Rsa97
@Rsa97
Для правильного вопроса надо знать половину ответа
Убедитесь, что ваш сервер правильно отвечает на запросы OPTIONS. Добавьте OPTIONS в список Access-Control-Allow-Methods.
CORS Preflight
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы