【发布时间】:2014-09-19 12:27:48
【问题描述】:
我需要解析一个由远程页面的 javascript 动态生成的内容。
比如我需要从这个页面获取价格:http://www.alibaba.com/product-detail/BRG-Newest-Fashional-Protective-Case-For_1666645206.html#J-wrapper
但是价格是由javascript生成的,所以当我用ajax下载页面时,它只下载html文件,没有脚本的结果。
所以我尝试在后台嵌入一个 iframe,然后解析这个 iframe 中的文档,但是存在一个安全问题,我无法解析它。
你知道我是否还有其他方法可以做到吗?
我使用的函数是这样的:
$.ajax({
url: url,
dataType: 'text',
success: function(html, _, xhr) {});
但生成的 HTML 没有脚本信息,因此价格为空。
我也试过用:
<html lang="en">
<head>
<meta charset="utf-8">
<title>contents demo</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<iframe src="http://api.jquery.com/" width="80%" height="600" id="frameDemo"></iframe>
<script>
document.getElementById("frameDemo").onload = function() {
var contents = $( "#frameDemo" ).contents();
}
</script>
</body>
</html>
但我收到此错误:
Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement':
Blocked a frame with origin "null" from accessing a frame with origin "api.jquery.com".
The frame requesting access has a protocol of "file", the frame being accessed has a protocol of "http".
Protocols must match.
【问题讨论】:
-
你的问题没有得到解决
-
做这个服务器端来防止同域源策略
-
我需要在客户端做,所以我得到了同域策略,还有其他方法吗?
标签: javascript iframe google-chrome-app