简体   繁体   English

如何使用javascript / jquery获取网址?

[英]how to fetch a url with javascript/jquery?

i need to fetch a url with javascript/jquery and not php. 我需要使用javascript / jquery而不是php获取网址。

i've read that you could do that if you got a php proxy, but that means that it is still going through php. 我读过,如果您有一个php代理,则可以这样做,但这意味着它仍在通过php。 cause then it's still the ip of the server that is fetching it. 因为那仍然是获取它的服务器的IP。

could one fetch the url entirely with only front-end, and thus fetch it with the client's ip? 一个人可以只用前端来获取URL,然后用客户端的IP来获取它吗?

There exists a Same origin policy for AJAX requests. AJAX请求存在一个“ 相同来源”策略 This prevents Javascript on, say, this site, from making a request to gmail.com (with your cookies), reading your e-mails, and uploading them to the StackOverflow server. 这样可以防止该站点上的Javascript向gmail.com发送请求(带有Cookie),阅读电子邮件并将其上传到StackOverflow服务器。 Javascript on stackoverflow.com can only make AJAX requests to pages on that domain. stackoverflow.com上的Java语言只能向该域上的页面发出AJAX请求。

As you can see, this is essential for security. 如您所见,这对于安全性至关重要。 Requests must instead be made by a proxy running on your web server - PHP can be used, but there are other solutions. 而是必须由运行在Web服务器上的代理发出请求-可以使用PHP,但还有其他解决方案。 For example, Ajax Cross Domain is an AJAX library that communicates with a Perl script running on the server to emulate AJAX requests for other domains. 例如, Ajax跨域是一个AJAX库,它与服务器上运行的Perl脚本进行通信,以模拟对其他域的AJAX请求。

It is also possible to make requests on other domains via a javascript include (script tag), image tag, etc. but in these cases you cannot read the contents of the page. 也可以通过javascript包含(脚本标签),图像标签等在其他域上发出请求,但在这些情况下,您将无法读取页面的内容。

You cannot do this with an iframe either: scripts cannot see the internals of iframes unless they are on the same domain as the script. 您也无法使用iframe进行此操作:脚本无法查看iframe的内部结构,除非它们与脚本位于同一域中。

So in short, use a proxy. 简而言之,请使用代理。

The problem is that jQuery would fetch an url with AJAX and AJAX won't operate cross-domain because of the potential security (as per the same-origin policy ). 问题是jQuery会使用AJAX来获取网址,并且由于潜在的安全性(根据同源策略 ),AJAX无法跨域运行。

There are however ways to emulate this, if you load the page in an iframe you can retrieve the data by using innerHTML on the iframe. 但是,有多种方法可以模拟这一点,如果将页面加载到iframe中,则可以通过使用iframe上的innerHTML来检索数据。 Here's an example script that uses jQuery: http://code.google.com/p/jquery-crossframe/ 这是使用jQuery的示例脚本: http : //code.google.com/p/jquery-crossframe/

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

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