Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 跨域jquery获取_Javascript_Jquery_Ajax_Twitter - Fatal编程技术网

Javascript 跨域jquery获取

Javascript 跨域jquery获取,javascript,jquery,ajax,twitter,Javascript,Jquery,Ajax,Twitter,我看到了一些使用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/jqu

我看到了一些使用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>

您不能使用
$.get
,因为这会执行一个ajax调用,该调用将是跨源的,因此会被阻止,并且您尝试访问的Twitter API不支持(或者如果支持,则不允许源
null
http://jsbin.com
,这是我尝试过的)

不过,API确实支持(这不是真正的ajax调用),因此只需将
$.get
更改为
$。ajax
指定
JSONP
即可:

$.ajax({
  url: url,
  dataType: "jsonp",
  success: function (data) {
    console.log(data)
    alert(data);
  }
});

|

你从哪里得到这个例子?不正确的是,您必须专门设置外部服务器以使用跨域AJAX,这在这里是无法做到的。此外,只有声明。这不是一个实际的问题。只是建议“bug在哪里”,确保您没有使用chrome和locahost进行测试。众所周知,Chrome不支持网络上的跨域请求localhost@JeremyBlalock:FWIW,该Twitter API不支持来自
http://jsbin.com
也是,所以我怀疑它不支持CORS。
$.ajax({
  url: url,
  dataType: "jsonp",
  success: function (data) {
    console.log(data)
    alert(data);
  }
});