https://stackoverflow.com/questions/35588699/response-to-preflight-request-doesnt-pass-access-control-check
总结来说,前端调试时可以分为以下解决方案
- Turn off CORS. 关闭浏览器cors,不太推荐
- 安装浏览器插件解决,如 allow-control-allow-origin 具体可以在google商城搜索
- Use a proxy such as nginx 本地nginx 服务解决跨域,主要配置如下
- 服务端增加配置,需要服务端配合
目前比较推荐使用第二种方案
upstream api_server {server apiserver.com;}server {charset UTF-8;listen 80;root /home/web/myclient;index index.html;server_name myclient.com;location /api/ {proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-NginX-Proxy true;proxy_pass http://api_server/;proxy_ssl_session_reuse off;proxy_set_header Host $http_host;proxy_redirect off;}location ~ /\. {deny all;}location / {try_files $uri;}}
