【发布时间】:2015-01-24 08:24:03
【问题描述】:
我有一个在 不同 服务器上运行的 Web 应用程序 (www.webapp.com),它试图通过我的后端休息服务器 (api.example.com) 进行 api 调用。我正在尝试通过 Web 浏览器与 Web 应用程序交互,但 ajax 调用不起作用。例如,尝试向 api.example.com/endpoint 发出请求。
知道我在这里缺少什么吗?
在 Chrome 的控制台上,我得到:
OPTIONS https://api.example.com/endpoint net::ERR_CONNECTION_TIMED_OUT
在 Firefox 的控制台上,我得到:
OPTIONS https://api.example.com/endpoint [250ms]
null
我正在使用 nginx 为其余服务器提供服务。 nginx.conf 的 location 部分包含:
location /{
proxy_pass http://127.0.0.1:5000;
add_header 'Access-Control-Allow-Header' 'Origin, Content-Type, Accept, Authorization';
add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS';
add_header 'Access-Control-Allow-Origin' 'http://example.com';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'http://www.webapp.com';
add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS';
add_header 'Access-Control-Max-Age' '1728000';
add_header 'Content-Type' 'text/plain; charset=UTF-8';
return 200;
}
【问题讨论】: