我怎么可以加载跨域的HTML页面使用jQuery AJAX? [英] How can I load cross domain html page with jQuery AJAX?
问题描述
如何加载跨域的HTML页面用jQuery AJAX?
How can I load cross domain HTML page with jQuery AJAX?
假设我希望得到一个页面使用我的域之外的jQuery AJAX:
Suppose I want to get a page outside my domain using jQuery AJAX:
$.get('http://www.domain.com/mypage.html', function(data) {
alert(data);
});
我可能会收到此错误信息:
I will probably get this error message:
XMLHtt prequest无法加载 http://www.domain.com/path/filename一>。起源 空不被访问控制 - 允许 - 产地允许的。
XMLHttpRequest cannot load http://www.domain.com/path/filename. Origin null is not allowed by Access-Control-Allow-Origin.
我们不能使用,因为相同的起源政策的AJAX加载跨域页面。
we can't load cross domain page using AJAX because of the Same-origin policy.
我可以尝试使用JSONP绕过这个限制:
I could try using 'jsonp' to bypass this restriction:
$.ajax({
type: "GET",
url: url,
dataType: "jsonp",
success: function(data){
console.log(data);
}
});
但是,如果'JSONP不支持这个网站是什么?这可能是一个问题。
But what if 'jsonp' is not supported in this site? this could be a problem.
如果我只是想读取外部网页,解析它的HTML?
What if I just want to read an external page and parse its HTML?
推荐答案
您可以使用 AJAX跨地'一个jQuery插件加载跨域的HTML页面。 AJAX十字起源是一个jQuery插件,允许跨地AJAX请求。 有了这个插件,我们可以很容易地绕过相同的起源并做跨域请求。
You can use 'AJAX Cross Origin' a jQuery plugin to load cross domain HTML page. AJAX Cross Origin is a jQuery plugin to allow Cross Origin AJAX requests. With this plugin we can easily bypass the Same-origin policy and do cross domain requests.
这是非常简单的使用方法:
It is very simple to use:
$.ajax({
crossOrigin: true,
url: url,
success: function(data) {
console.log(data);
}
});
您可以阅读更多关于它在这里: http://www.ajax-cross-origin.com /
You can read more about it here: http://www.ajax-cross-origin.com/
这篇关于我怎么可以加载跨域的HTML页面使用jQuery AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!