我可以搜索谷歌与XMLHtt prequest()?XMLHtt、prequest

2023-09-10 20:45:37 作者:王位之上

我可以搜索谷歌有一个跨域XMHhtt prequest()?

  VAR XHR = XMLHtt prequest();
xhr.open(GET,www.google.com/?q=what+you+want+to+search,真正的);
 

解决方案

试试看:

卷曲-H产地:http://domain.com-X选项--head https://www.google.com/

目前这个给你:

  HTTP / 1.1 405不允许的方法
内容类型:text / html的;字符集= UTF-8
内容长度:962
日期:周五,2013年6月21日十七时58分45秒格林尼治标准​​时间
服务器:GFE / 2.0
 

所以,不,你不能,至少不是他们面向公众的网站。将会有一个访问控制 - 允许 - 产地:* 在那里,如果是这样的话,用200 OK。该 * 是一个通配符任何域。因此,这将要么是这个,或者它会以符合您的由来。

即使它没有返回正确的标题,你必须有CORS支持在浏览器中。你可以看到浏览器的兼容性这里。 IE 8和9只支持CORS通过 XDomainRequest ,其中有重限制(无Cookie或自定义标题,例如)。你可以阅读更多关于这里 访问控制头。

httos双向认证配置 安卓APP抓包之双向认证突破

在CORS失败,同源策略的使用。

有虽然一种方式。该谷歌REST API 不支持跨起源请求:

卷曲-H产地:http://domain.com-X GET --headhttps://www.googleapis.com/customsearch/v1?"

它给你:

  HTTP / 1.1 400错误的请求
访问控制 - 允许 - 产地:http://domain.com
内容类型:应用程序/ JSON;字符集= UTF-8
访问控制 - 揭露-头:内容编码,内容长度,内容类型,服务器
日期:周五,2013年6月21日十八时12分51秒格林尼治标准​​时间
到期日:周五,2013年6月21日18时12分51秒格林尼治标准​​时间
缓存控制:私人,最大年龄= 0
的X内容类型选项:nosniff
X框选项:SAMEORIGIN
的X XSS-保护:1;模式=块
服务器:GSE
传输编码:分块
 

注意访问控制 - 允许 - 产地:http://domain.com

因此​​,假如你有一个API密钥,你可以,如果你使用的API。

Can I search Google with a cross-origin XMHhttpRequest()?

var xhr = XMLHttpRequest();
xhr.open("GET", www.google.com/?q=what+you+want+to+search, true);

解决方案

Try it:

curl -H "Origin: http://domain.com" -X OPTIONS --head https://www.google.com/

This currently gives you:

HTTP/1.1 405 Method Not Allowed
Content-Type: text/html; charset=UTF-8
Content-Length: 962
Date: Fri, 21 Jun 2013 17:58:45 GMT
Server: GFE/2.0

So no, you can't, at least not with their public facing website. There would be an Access-Control-Allow-Origin: * in there if that was the case, with a 200 OK. The * is a wildcard for "any domain". So it would either have to be this, or it would have to match your origin.

Even if it did return the correct header, you have to have CORS support in the browser. You can see browser compatibility here. IE 8 and 9 only supports CORS through XDomainRequest, which has heavy restrictions (no cookies, or custom headers, for example). You can read more about access control headers here.

When CORS fails, same origin policy is used.

There is a way though. The Google REST API does support cross origin requests:

curl -H "Origin: http://domain.com" -X GET --head "https://www.googleapis.com/customsearch/v1?"

Which gives you:

HTTP/1.1 400 Bad Request
Access-Control-Allow-Origin: http://domain.com
Content-Type: application/json; charset=UTF-8
Access-Control-Expose-Headers: Content-Encoding,Content-Length,Content-Type,Server
Date: Fri, 21 Jun 2013 18:12:51 GMT
Expires: Fri, 21 Jun 2013 18:12:51 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Transfer-Encoding: chunked

Notice the Access-Control-Allow-Origin: http://domain.com.

So assuming you have an API key, you can, if you use the API.