简体   繁体   English

AJAX请求从Google获取结果

[英]AJAX request to get results from Google

I want to modify the code in this AJAX example to get results from Google. 我想修改此AJAX示例中的代码以从Google获得结果。

I take it out the line with the Random() method call, but how do I work with the query string in the JavaScript? 我通过Random()方法调用将其删除,但是如何在JavaScript中使用查询字符串呢? How can I best do this using XMLHttpRequest to manually build the request (this is for testing so has to be manually done, as opposed to using a framework). 如何最好地使用XMLHttpRequest手动构建请求(这是为了测试,因此必须手动完成,而不是使用框架)。

Thanks 谢谢

You cannot use XMLHttpRequest with an absolute URL like http://www.google.com/... . 您不能将XMLHttpRequest与绝对URL一起使用,例如http://www.google.com/...

The browser will prevent cross-site scripting . 浏览器将阻止跨站点脚本编写 You have to use a relative path, otherwise most browsers will simply return an empty responseText . 您必须使用相对路径,否则大多数浏览器将只返回一个空的responseText

As one possible workaround, you could set up a very simple reverse proxy (using mod_proxy if you are on Apache). 作为一种可能的解决方法,您可以设置一个非常简单的反向代理 (如果使用Apache,则使用mod_proxy )。 This would allow you to use relative paths in your AJAX request, while the HTTP server would be acting as a proxy to any "remote" location. 这将允许您在AJAX请求中使用相对路径,而HTTP服务器将充当任何“远程”位置的代理。

The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. mod_proxy中设置反向代理的基本配置指令是ProxyPass。 You would typically use it as follows: 您通常按以下方式使用它:

ProxyPass     /ajax/     http://www.google.com/search

In this case, the browser would be requesting /ajax/?hl=en&source=hp&q=test but in fact the server would serve this by acting as a proxy to http://www.google.com/search?hl=en&source=hp&q=test . 在这种情况下,浏览器将请求/ajax/?hl=en&source=hp&q=test但实际上服务器将通过充当http://www.google.com/search?hl=en&source=hp&q=test

If you are using IIS, you may want to use the Managed Fusion URL Rewriter and Reverse Proxy to set up a reverse proxy. 如果使用的是IIS,则可能要使用托管融合URL重写器和反向代理来设置反向代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM