我有一个来自服务器的请求(NGINX服务于Angular),该服务器从给定的经纬度中查找最近的学校,医院和交通工具。
执行请求时,我在服务器上收到CORS标头错误(见下文):
在以下位置访问XMLHttpRequest 来源“ https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=long,lat&radius=500&type=school&keyword=&key=APIKey”中的“ https://domainname.co.uk”已被阻止 CORS政策:在“ 请求的资源
该站点托管在NGINX(Docker容器)上,我已将conf文件修改为以下内容:
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
location / {
add_header Access-Control-Allow-Origin *;
try_files $uri $uri/ /index.html;
}
}
}
据我了解,add_header行应允许Access-Control-Allow-Origin,但我仍然不断收到上述错误。
感谢您的帮助。
答案 0 :(得分:0)
我设法解决了这个问题,以防万一有人再次遇到此问题,答案是使用客户端库(而不是服务器端库)。看这里: XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' Google maps