简体   繁体   English

如何从JavaScript文件获取JSON数据

[英]How to get json data from javascript file

help, im using tumblr and connect my twitter account, Tumblr gave me this file example: http://titan-theme.tumblr.com/tweets.js 帮助,即时通讯使用tumblr并连接我的Twitter帐户,Tumblr给了我这个文件示例: http : //titan-theme.tumblr.com/tweets.js

my question is can i get follower_count and screen_name data? 我的问题是我可以获取follower_count和screen_name数据吗? if yes how to get it? 如果是,如何获得? please help me thanks 请帮我谢谢

If you have only this file you should define *recent_tweets* function. 如果只有此文件,则应定义* recent_tweets *函数。 Of cours you need to import tweets.js. 当然,您需要导入tweets.js。 For example: 例如:

<script>
    var recent_tweets = function(tweets) {
        for(var i=0; i<tweets.length; i++) {
            var tweet = tweets[i];
            var followerCounts = tweet.user.followers_count;
            var screenName = tweet.user.screen_name;
        }
    }   
</script>
<script src="tweets.js"></script>

However, there is no follower_count available. 但是,没有可用的follower_count。

This is a JSONP file, it needs to be requested via a script tag in order to execute the recent_tweets function that encloses it. 这是一个JSONP文件,需要通过脚本标签进行请求才能执行将其封装起来的recent_tweets函数。 Try using the jQuery ajax function. 尝试使用jQuery ajax函数。

$.ajax({
  dataType: "jsonp",
  success: function (result) {
    callback(result);
  }
});

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

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