简体   繁体   中英

ASP.NET MVC4 & JQuery - Twitter Feed Script not working

Here is my exact _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/Scripts/Tweets.js")
    @RenderSection("Scripts",false)
    <script type="text/javascript" src="http://cdn.jquerytools.org/1.1.2/full/jquery.tools.min.js"></script>
    <script src="../../Scripts/Tweets.js" type="text/javascript"></script>
</head>
<body> 
    @{
        if (ViewBag.IsAdmin ?? false)
        {   
            <div class="controlPanel">
                <a href="@Href("~/Post/Edit")">New Post</a>

            @Html.ActionLink("Sign Out", "DeAuth", "Account")
            </div>
        }
        else
        {
            <div class="controlPanel">
                @Html.ActionLink("Log In", "Login", "Account")
            </div>
        }
    }
    <header>
        <a href="@Href("~/")"><img src="@Href("~/Content/Images/gizmologo.png")" alt="Gizmo | Blog Solutions" title="Gizmo | Blog Solutions"/></a>
    </header>
    <section>
        <div id="topSep" class="sep"></div>
        <aside>
            <img id="picture" width="100" height="100" src="@Href("~/Content/Images/dsykes.jpg")" alt="Picture" title=":P" />
            <div id="twitterHeader"><a href="https://twitter.com/Sage0f_Lyricism">D.Sykes on Twitter</a></div>
            <div id="tweets">



            </div>
            <div id="feedLink"><a href="@Href("~/Post/RSS")">RSS Feed</a></div>
        </aside>
        <div id="main">
            @RenderBody()
        </div>

        <div id="bottomSep" class="sep"></div>
    </section>

    <footer>
        <div id="copyright">Copyright &copy; 2013, Gizmo Bloging</div>      
    </footer>


</body>
</html>

And here is the Tweets.js Script that wont load...

$(document).ready(function () {
    //$.getJSON("https://twitter.com/statuses/user_timeline.json?screen_name=ninanet&count=5&callback=?", // this is the OLD Twitter json call!
    $.getJSON("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=ninanet&count=5&callback=?",
    function (data) {
        $.each(data, function (i, item) {
            ct = item.text;
            mytime = item.created_at;
            var strtime = mytime.replace(/(\+\S+) (.*)/, '$2 $1')
            var mydate = new Date(Date.parse(strtime)).toLocaleDateString();
            var mytime = new Date(Date.parse(strtime)).toLocaleTimeString();

            $("#tweets").append('<div>' + ct + " <small><i>(" + mydate + " at " + mytime + ")</i></small></div>");
        });
    });

});

I dont know what could be the problem, my Jquery is initialized properly and so is the script but i dnt get anything.. Can someone pleeease help!?

Working demo: http://jsfiddle.net/JTrs7/

Cool, if you are trying to fetch data cross domain try using JSONP

Helpful links: ( JSONP vs JSON )

Hope it fits your needs :)

Sample code

$(document).ready(function () {

    var k = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Twitter&include_rts=1&count=5&jsoncallback=";

    $.ajax({
        dataType: 'jsonp',
        url: k,
        success: function (data) {
            console.log(data);
            $.each(data, function (i, item) {
                $("#tweetFeed").append("<div class='tweetCloud'><div id='tweetArrow'></div><div id='tweetText'>" + item.text + "</div></div>");
            })
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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