views:

83

answers:

2

I have this html:

<input type="text" id="text"/>
<input type="button" id="submit" value="Submit" />
<div id="twitter_update_list">
</div>

and this javascript:

var xmlHttp;
document.body.onclick = function(){
    var username = document.getElementById('text').value;
    selectUser(username);
}
    function selectUser(username){
        var url = "http://twitter.com/statuses/user_timeline/" + username + ".json?callback=twitterCallback2&count=100";
        try{// Opera 8.0+, Firefox, Safari
            xmlHttp = new XMLHttpRequest();
        }catch (e){// IE
            try{
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    // Something went wrong
                    alert("Your browser broke!");
                    return false;
                }
            }
        }
        xmlHttp.onreadystatechange = processRequest;
        xmlHttp.open( "GET", url, true );
        xmlHttp.send( null );
    }

    function processRequest(){
        if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)) {
            if ( xmlHttp.responseText == "Not found" ) {
                document.getElementById('twitter_update_list').innerHtml = "Not found";
            }else if(xmlHttp.responseText == " "){
                document.getElementById('twitter_update_list').value = "Empty";
            }else{
                // No parsing necessary with JSON!        
                document.getElementById('twitter_update_list').value = xmlHttp.responseText;
                console.log(xmlHttp.responseText);
            }
        }
    }

I am looking at firebug and I see verything gets sent correctly but I don't get any response whatsoever. Oh, and I'm using raw javascript because I want to practice it. =)

+1  A: 

You are trying to do cross-domain XMLHttpRequest. It doesn't work although the service may be supporting JSONP. To do a JSOP request, you either need to inject a <script> tag with the correct event handlers or use a framework such as jQuery.

Chetan Sastry
+1: Can't believe I didn't look at the URL.
Max Shawabkeh
Unless he works for twitter :)
Chetan Sastry
A: 

If you use jquery you could simple do this

$.load("http://twitter.com/statuses/user_timeline/"+username,
function(data)
{
alert(data);
}
);

which will print the usertimeline for the given username

streetparade