首页 > 关于xmlhttprequest 如何发跨域请求?

关于xmlhttprequest 如何发跨域请求?

我在http://www.tudou.com 这个网站上 打开控制台 输入如下命令:

var xhr = new XMLHttpRequest;
url = 'http://saic-sis.escdn.com/api.php?op=get_linkage_xjw&parentid=10000000000004&act=ajax_getlist&keyid=1&callback=cb';

xhr.open('GET', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send();

xhr.onreadystatechange = function() {
if (xhr.status === 200) {

    alert(200);

}else {

alert(xhr.statusText);

}
}

看到控制台有错误 :XMLHttpRequest cannot load http://saic-sis.escdn.com/api.php?op=get_linkage_xjw&parentid=10000000000004&act=ajax_getlist&keyid=1&callback=cb. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.tudou.com' is therefore not allowed access.

我查了下说是跨域问题, 请问jsop到底是怎么发请求?

$.getJSON()方法我以为是 会创建一个script标签然后掉回调函数,但为什么我的控制台看不到有新增的script标签, 很想知道这个方法大概是怎么跨域。


XMLHttpRequest的跨域,是需要对方网站支持的,对方网站有相应的允许跨域的响应标头才可以。
JSONP的跨域实际上不是用XHR实现的,而是一个script标签,script是可以跨域的,回调函数作为get参数传入请求里。
总之,不论是XHR的跨域,还是JSONP,都是需要对方网站提供支持,在对望愿意分享给你数据的情况下你才能得到。
在对方没有提供支持的情况下,你是取不到它的数据的

【热门文章】
【热门文章】