Ajo*_*uve 18 javascript twitter ajax jquery
我看到了一些带有ajax的跨域示例,但它不起作用.
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<script type="text/javascript" >
$(document).ready(function () {
var url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=AssasNet&include_rts=1";
$.get(url, function (data) {
console.log(data)
alert(data);
});
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我尝试使用chrome并给出以下错误:
XMLHttpRequest cannot load http://api.twitter.com/1/statuses/user_timeline.json?screen_name=AssasNet&include_rts=1. Origin null is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
T.J*_*der 42
您不能使用,$.get因为它执行ajax调用,它将是跨源的并因此被同源策略阻止,并且您尝试访问的Twitter API不支持跨源资源共享(或者如果它是的,它不允许任何来源null或http://jsbin.com,这是我试过的那个).
API确实支持JSONP(这不是真正的ajax调用),所以只需更改$.get为$.ajax指定的JSONP工作:
$.ajax({
url: url,
dataType: "jsonp",
success: function (data) {
console.log(data)
alert(data);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55780 次 |
| 最近记录: |