什么$ P $使用$就加载另一个域的HTML pvents我吗? [英] What prevents me from using $.ajax to load another domain's html?
问题描述
我的域名:
<!DOCTYPE HTML>
< HTML>
< HEAD>
<冠军>刮< /标题>
&所述;脚本的src =的http://$c$c.jquery.com/jquery-1.7.1.min.js>&所述; /脚本>
< /头>
<身体GT;
<脚本>
$阿贾克斯(网址:{url:HTTP://their-domain.com/index.html,
数据类型:HTML,
成功:功能(数据){执行console.log(数据);}
});
< / SCRIPT>
< /身体GT;
< / HTML>
什么$ P $从能够刮掉自己的域pvents我吗?任何变通?
附录:谢谢大家使用服务器端脚本的建议,但我一时兴趣使用的客户端完全解决这个问题。
。如果我使用JSONP格式化要求我至少得到一个答复,但有以下错误:未捕获的SyntaxError:意外的标记<。所以,我正在从他们的域的回应,但解析器希望它是JSON。 (除了它应该。)我通过这个黑客想看看他们是一种欺骗客户到接受此响应。请理解,我知道这是非典型的。
<!DOCTYPE HTML>
< HTML>
< HEAD>
<冠军>刮< /标题>
&所述;脚本的src =的http://$c$c.jquery.com/jquery-1.7.1.min.js>&所述; /脚本>
< /头>
<身体GT;
<脚本>
$阿贾克斯(网址:{url:HTTP://their-domain.com/index.html,
数据类型:JSONP,
成功:功能(数据){执行console.log(数据);}
});
< / SCRIPT>
< /身体GT;
< / HTML>
有四种方法可以解决相同原产地政策
- 在代理 - 您可以从服务器请求它,你的服务器从其他域要求它,您的服务器返回给浏览器
- 的Flash跨域策略 - 其他域必须添加一个crossdomain.xml文件他们的网站
- 跨域HTTP头 - 其他域必须添加一个
访问控制 - 允许 - 原产地
头到他们的页面 - JSONP - 这是一个JSON的Web服务,它提供了一个回调函数。其他领域必须实现这一点。
注:仅办法做到这一点没有其他域的帮助#1,通过自己的服务器路由,
My domain:
<!DOCTYPE html>
<html>
<head>
<title>scrape</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
<script>
$.ajax({url:'http://their-domain.com/index.html',
dataType:'html',
success:function(data){console.log(data);}
});
</script>
</body>
</html>
What prevents me from being able to scrape their-domain? Any work around?
Addendum: thank you all for the suggestions to use a server side script, but I am for the moment interested in solving this problem exclusively using the client.
If I format the request using "jsonp" I do at least get a response, but with the following error:"Uncaught SyntaxError: Unexpected token <". So I am getting a response from their-domain but the parser expects it to be json. (As well it should.) I am hacking through this trying to see if their is a way to trick the client into accepting this response. Please understand that I know this is atypical.
<!DOCTYPE html>
<html>
<head>
<title>scrape</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
<script>
$.ajax({url:'http://their-domain.com/index.html',
dataType:'jsonp',
success:function(data){console.log(data);}
});
</script>
</body>
</html>
There are Four ways to get around Same Origin Policy
- Proxy - You request it from your server, your server requests it from other domain, your server returns it to the browser
- Flash cross domain policy - other domain must add a crossdomain.xml file to their site
- Cross domain HTTP header - other domain must add an
Access-Control-Allow-Origin
header to their page - JSONP - It's a json web service that provides a callback function. Other domain must implement this.
Note: The ONLY way to do it without the other domain's help is #1, routing it through your own server.
这篇关于什么$ P $使用$就加载另一个域的HTML pvents我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!