当我的网站通过谷歌翻译运行时,我遇到了麻烦:我的服务器根本没有回复我的浏览器的AJAX请求,而不是单个标题。
请求网址如下:
Google翻译会发送一个origin
请求标头,其值为https://translate.googleusercontent.com
。
我做了很多尝试,这里基本上是我试图通过PHP设置的标题响应:
header('Access-Control-Allow-Origin: https://translate.googleusercontent.com');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Origin: translate.googleusercontent.com');
header('Access-Control-Allow-Origin: googleusercontent.com');
header('Access-Control-Allow-Origin: google.com');
显然,*
值最不安全,但没有工作需要进行更激烈的实验。我也(没有成功)将此添加到AJAX请求中:
//This is now removed due to research revealing this is why Chrome was making the request as
//an HTTP OPTIONS method.
if (window.location.host!=domain) {xhr.setRequestHeader('Access-Control-Allow-Origin','*');}
我已将PHP设置为通过Google翻译请求向我发送$_SERVER
转储:
Array
(
[GATEWAY_INTERFACE] => CGI/1.1
[HTTPS] => on
[HTTP_ACCEPT] => *
[HTTP_ACCEPT_CHARSET] => utf-8,*
[HTTP_ACCEPT_ENCODING] => gzip,deflate,br
[HTTP_CONNECTION] => Keep-alive
[HTTP_HOST] => www.example.com
[HTTP_USER_AGENT] => ***
[HTTP_VIA] => 1.0 translate.google.com TWSFE/0.9
[HTTP_X_FORWARDED_FOR] => [my IP], [my IP...again]
[PATH] => /bin:***
[PHPRC] => ***
[QUERY_STRING] => query=index.php
[REDIRECT_STATUS] => 200
[REMOTE_ADDR] => 66.102.8.203 [Google Proxy]
[REMOTE_PORT] => ***
[REQUEST_METHOD] => GET
[REQUEST_URI] => /
[SCRIPT_FILENAME] => index.php
[SCRIPT_NAME] => /index.php
[SERVER_ADDR] => ***
[SERVER_ADMIN] => ***
[SERVER_NAME] => www.example.com
[SERVER_PORT] => 443
[SERVER_PROTOCOL] => HTTP/1.1
[SERVER_SIGNATURE] =>
[SERVER_SOFTWARE] => Apache
[SSL_TLS_SNI] => www.example.com
[UNIQUE_ID] => ***
[PHP_SELF] => /index.php
[REQUEST_TIME_FLOAT] => ***
[REQUEST_TIME] => ***
)
Firefox使用HTTP POST
方法正确发出请求。出于某种原因,Chrome使用HTTP OPTION
方法发出AJAX请求...等等什么?这不是我的网站编码方式。
我怀疑服务器可能正在响应Google的初始请求服务器,而不是我的浏览器,尽管我不确定。 $_SERVER
包含$_SERVER['HTTP_X_FORWARDED_FOR']
我的IP地址列出两次,但我不确定这一切是如何运作的。我猜这比普通的CORS请求要复杂一些。如何让我的服务器响应我的浏览器?