listen 9099;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Allow-Methods' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers:x-requested-with,Content-Type,token' '*';
if ($request_method = 'OPTIONS') {
return 200;
}
location / {
root html/upfile;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
打开浏览器控制台,分析有哪些请求头和需要的响应头;贴一下报错截图;如果nginx暂时没找到原因,可以后端程序中跨域处理,
php为例,无非是这几个
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
$allowOrigin = [
'https://xx.com',
'https://xx2.com',
'http://xx.com',
];
if (in_array($origin, $allowOrigin)) {
header("Access-Control-Allow-Origin:".$origin);
}
header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With');
header('Content-type: text/json; charset=utf-8');
header('Access-Control-Allow-Methods: POST,GET');
上传的方式改下或者可以实现跨域