Node.js:从不同域加载页面的 html
我想知道如何加载托管在不同域上的 HTML?
我正在使用 JavaScript,并且想要创建一个书签,以便我能够解析外部 HTML。
我已经在谷歌上搜索了几个小时,但毫无结果......
I was wondering how can I load HTML, which is hosted on a different domain?
I am using JavaScript, and want to create a bookmarklet that will enable me to parse the external HTML.
I have been googling for hours in vain...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JavaScript 不允许发出跨域请求。 这是一个很大的安全风险。 相反,您必须在服务器上执行脚本并将结果返回到 JavaScript 函数。
例如,假设您使用 JavaScript 和 PHP,您可以将应用程序设置为按如下方式工作:
JavaScript 向位于服务器上的页面(或脚本)发起 Ajax 请求。 它将任何必需的参数传递到此页面。 以下代码基于 jQuery(为了简洁起见),但无论您的框架如何,原理都是相同的。
服务器端代码将响应请求并将必要的参数传递给跨域网站。 PHP 的 cURL 库 对此很有用。
当跨域网站响应时,您的服务器端代码可以将响应回显到初始请求。 这可能应该在回复之前进行验证,但这只是一个例子。
然后,JavaScript 响应处理函数可以解析传入的响应数据。 请注意上面第一段 JavaScript 代码中的 success 函数。
JavaScript isn't allowed to make cross-domain requests. It's a big security risk. Instead, you'll have to execute a script on the server and have it return the results to your JavaScript function.
For example, assuming that you're using JavaScript and PHP you could setup the application to work like this:
JavaScript initiates an Ajax request to a page (or script) located on your server. It passes any required parameters to this page. The following code is based on jQuery (for the sake of being concise), but the principles are the same regardless of your framework.
The server-side code will respond to the request and pass along the necessary parameters to the cross-domain website. PHP's cURL library is good for this.
When the cross-domain website responds, your server-side code can echo the response back to the initial request. This should probably be validated before responding back, but it's just an example.
A JavaScript response handler function can then parse the incoming response data. Note the success function in the first block of JavaScript code above.
如果您不拥有其他页面,那将非常困难。
事实上,你不能在 JavaScript 中向另一个域发出请求。 您唯一能做的就是从另一个域加载脚本:
如果这样做的目的是从另一个域加载数据,并且(正如我所说)您拥有另一个站点,则可以创建 script.js 文件,该文件将在您的原始站点中加载您需要的数据。
请记住,这是一件微妙的事情,只有在您知道脚本中将包含什么内容时才应该这样做。
If you don't own the other page, it'll be very difficult.
In fact, you can't make a request to another domain in javascript. The only thing you can do is load a script from another domain:
If the purpose of this is to load data from the other domain, and (as I said) you own the other site, you can create the script.js file, which will load the data you need in your original site.
Remember this is a delicate thing, and should be done only if you know what there will be in the script.
您可以从
localhost
发出跨域请求,但如果您打算将此代码部署到服务器上,那么它就行不通。 由于您正在开发一个书签,我认为您可以做到这一点。您需要使用 AJAX 来获取远程 HTML。
jQuery 库使这项任务变得如此简单......
You can make cross-domain requests from
localhost
, but if you plan to deploy this code to a server, it's not going to work. Since you are developing a bookmarklet, I think you can do this.You'll need to use AJAX to get the remote HTML.
The jQuery library makes this task as simple as this...