通过Google Translate访问网站时,没有服务器对AJAX请求的响应

时间:2017-01-23 20:43:59

标签: javascript php cors google-translate

当我的网站通过谷歌翻译运行时,我遇到了麻烦:我的服务器根本没有回复我的浏览器的AJAX请求,而不是单个标题。

请求网址如下:

https://translate.google.com/translate?depth=1&hl=en&rurl=translate.google.com&sl=en&tl=sq&u=https://www.example.com/

Google翻译会发送一个origin请求标头,其值为https://translate.googleusercontent.com

我做了很多尝试,这里基本上是我试图通过PHP设置的标题响应:

  1. header('Access-Control-Allow-Origin: https://translate.googleusercontent.com');

  2. header('Access-Control-Allow-Origin: *');

  3. header('Access-Control-Allow-Headers: X-Requested-With');

  4. header('Access-Control-Allow-Origin: translate.googleusercontent.com');

  5. header('Access-Control-Allow-Origin: googleusercontent.com');

  6. header('Access-Control-Allow-Origin: google.com');

  7. 显然,*值最不安全,但没有工作需要进行更激烈的实验。我也(没有成功)将此添加到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请求要复杂一些。如何让我的服务器响应我的浏览器?

0 个答案:

没有答案