当我尝试从API请求信息时,我遇到了CORS策略问题。API位于不同的域和操作系统(Linux)中。HTML位于不同的域和操作系统(Windows)中。我尝试使用Postman检查该方法是否有效,在两台服务器中,Postman都运行良好,但当我移动到HTML时,我遇到了CORS策略问题。
更新:我设法修复了Nginx中的身份验证问题,但现在我面临着内容类型问题,不知道我需要修复的问题。我在Nginx中添加了内容类型,但它不工作
下面是当我运行按钮来运行jQuery代码时我得到的错误
CORS策略已阻止从源“http://127.0.0.1:5500”在“https://{域名}/API/connect/308”进行提取的访问:Access-Control-Allow-Headers不允许使用请求标头字段内容类型
下面是我用来从服务器获取请求的代码
jQuery(".chat-icon").click(function () {
var userId = "308";
fetch(url + "connect/" + userId, {
method: "GET",
mode: "cors",
headers: new Headers({
"Access-Control-Allow-Methods": "POST,GET,OPTIONS, PUT, DELETE",
"Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept, Authorization",
"auth": "123456",
"Content-Type": "application/json; charset=UTF-8",
}),
})
.then(response => response.json())
.then(json => {
jQuery("#starter").text(json.data);
jQuery("#displayDateTime").text(today);
$("#dt").text(moment().format('hh:mm:ss a'));
});
以下是fastAPI设置
orgins=['{certain path}','http//127.0.0.1:5500']
下面是nginx配置
location /api/ {
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type:application/json; charset=UTF-8,auth';
add_header 'Content-Type' 'application/json charset=UTF-8';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
#
# Om nom nom cookies
#
add_header 'Access-Control-Allow-Credentials' 'true';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
#
# 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;
}
include proxy_params;
proxy_pass http://127.0.0.1:5858;
}
1条答案
按热度按时间mv1qrgav1#
错误似乎是
Content-Type:application/json
不是头文件名,而是头文件名和值。