简体   繁体   English

将JSON格式的Twitter REST API与AJAX一起使用

[英]Using Twitter REST API in JSON Format with AJAX

I'm trying to do AJAX on Twitter REST API (JSON Format). 我正在尝试在Twitter REST API(JSON格式)上执行AJAX。 I did the below thing, But, nothing happens. 我做了以下事情,但是什么也没发生。

Not getting any response from server (Twitter). 无法从服务器(Twitter)获得任何响应。 Did I miss something? 我错过了什么?

function getRecentTweet(){

        twitterUsername = document.getElementById("twitterUsername").value;

        if(window.XMLHttpRequest){
            xhr = new XMLHttpRequest();
        }
        else{
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xhr.open("GET", "http://api.twitter.com/1/statuses/user_timeline/" + twitterUsername + ".json", true);
        xhr.send(null);

        xhr.onreadystatechange = function(){
            if (xhr.readyState == 4) {
                if(xhr.status == 200) {
                    var jsonObj = eval(responseJSON);
                    document.getElementById("twitterStream").innerHTML = jsonObj[0].text;
                }
            }
        } 
    }

Thanks! 谢谢!

Yes, cross-domain AJAX calls are not allowed. 是的,不允许跨域AJAX调用。 You need to use JSONP. 您需要使用JSONP。

Make a request to Twitter's API and append the callback parameter. 向Twitter的API发出请求,并附加callback参数。 Here's a sample url: 这是一个示例网址:

http://api.twitter.com/1/statuses/user_timeline/hulu.json?callback=myFunction http://api.twitter.com/1/statuses/user_timeline/hulu.json?callback=myFunction

The response from Twitter then will contain executable JavaScript code with the name of the function you specified as the callback parameter. 然后,来自Twitter的响应将包含可执行JavaScript代码,其中包含您指定为回调参数的函数的名称。 So Twitter's response looks like: 因此,Twitter的响应如下所示:

myFunction(<JSON here>);

Since cross-domain is an issue, you need to add a script tag to the page, and set the src attribute to the above URL. 由于跨域是一个问题,因此您需要在页面上添加脚本标签,并将src属性设置为上述URL。 This can be created and injected into the page dynamically with JavaScript. 可以使用JavaScript动态创建并注入到页面中。

<script src=" ... hulu.json?callback=myFunction"></script>

Then define a global function myFunction on the page that always gets called whenever the response from Twitter returns. 然后在页面上定义一个全局函数myFunction ,每当来自Twitter的响应返回时,该函数总是被调用。 This too can be predefined or dynamically generated. 这也可以是预定义的或动态生成的。

<script>
function myFunction(data) {
    console.log(data);
}
</script>

You may want to avoid using eval as well and utilize JSON.parse() method. 您可能还想避免使用eval并使用JSON.parse()方法。

If you want you can get the response, save it to a local json file server side, and access it from there utilizing AJAX. 如果希望得到响应,请将其保存到本地json文件服务器端,然后使用AJAX从那里访问它。

So you could call your server side script and avoid using JSONP which is executed JavaScript code. 因此,您可以调用服务器端脚本,而避免使用执行JavaScript代码的JSONP。 Twitter seems safe enough but you never know after LinkedIn, Yahoo, security breaches. Twitter似乎足够安全,但是您对LinkedIn,Yahoo,安全漏洞一无所知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM