【发布时间】:2021-02-19 22:22:17
【问题描述】:
我的情况的小背景:
我的 VPS 上有一个具有公共 IP 的应用程序。 VPS 基于 centOS7,我想通过 fetch 从我的 JavaScript 客户端向服务器发送 POST 查询。从服务器端,我使用烧瓶接收 POST 数据。
服务器端httpd.conf:
<Directory "/var/www/html/wet-pogotowie.pl">
<IfModule mod_headers.c>
AllowOverride All
Require all granted
Header add Access-Control-Allow-Origin "https://wet-pogotowie.pl"
Allow from all
</IfModule>
</Directory>
烧瓶代码:
from flask import Flask, request, jsonify
import json
app = Flask(__name__)
@app.route("/api", methods=['GET', 'POST'])
def json():
content = request.json
return content
context = ('/var/.certs/cert.crt', '/var/.certs/priv.key')
if __name__ == '__main__':
app.run(host = 'wet-pogotowie.pl', port = 443, ssl_context = context, debug = True)
JS 的 fetch:
time = setInterval(function() {
var zzz = {"xyz": "123"};
const data = JSON.stringify(zzz);
const options = {
method: 'POST',
body: data,
mode: 'cors',
headers: {
'content-type': 'application/json',
'Access-Control-Allow-Origin': 'https://wet-pogotowie.pl',
'Access-Control-Request-Method': 'GET, POST, DELETE, PUT, OPTIONS',
'Access-Control-Allow-Credentials': 'true',
crossorigin: true,
},
};
fetch('https://wet-pogotowie.pl/api', options) // TU DZILA, NIE DZIALA Z PORTEM
.then(response => response)
.then(data => {
// if everting is ok should log the object message: "Long lang sent to express" from server
console.log(data)
});
}, 9000);
问题是: 我无法在烧瓶中设置端口 443,因为它当然被占用了,但是当我尝试在 fetch 中设置其他端口时 - 'SAME ORIGIN POLICY' - 出现 CORS 错误。
我的问题: 我需要改变的地方和内容?我是否需要更改代码或服务器端的某些内容?
其他信息: 当 fetch 设置为默认 80 端口时 - POST 有效,浏览器中的控制台登录显示它,但收到 404 错误。
更新:
我已将服务器从 apache 更改为 nginx,这是nginx.conf 文件的一部分:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name www.wet-pogotowie.pl;
root /var/www/html/www.wet-pogotowie.pl;
# process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
ssl_certificate "/var/.certs/cert.crt";
ssl_certificate_key "/var/.certs/priv.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
location /api
{
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
proxy_pass https://www.wet-pogotowie.pl:3000;
}
main.js:
time=setInterval(function(){
const data = "patrz wredny"
const options = {
method: 'get',
mode: 'no-cors',
headers: {
'content-type': 'application/json',
'Access-Control-Allow-Origin': 'wet-pogotowie.pl',
'Access-Control-Request-Method': 'GET, POST, DELETE, PUT, OPTIONS',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'wet-pogotowie.pl',
},
}
fetch('https://wet-pogotowie.pl:3000/api',options)
.then(response => response)
.then(data => {
// if everting is ok should log the object message: "Long lang sent to express" from server
console.log(data)
});
},9000);
Flask 代码是相同的 - 只有 diff 端口设置为 3000 并启用 SSL_CONTEXT。 我的观点是 - 如何从 JS 的前端向 python 的烧瓶发送或接收 GET/POST?
【问题讨论】:
标签: javascript python flask fetch httpd.conf