Rectangle 27 0

I made a request to the Twitter API. How can I get the JSON response from my PHP script to my JavaScript?


function loadXMLDoc()
{
    var xmlhttp;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }

    else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {   
            var json = JSON.parse(xmlhttp.responseText);

            //log entire json struct to developer console for easy viewing
            console.log(json);

            //you can reference individual pieces of json by doing something like
            //json.statuses or json.statuses[2]
            var statuses = json.statuses;

            for(var i=0;i<statuses.length;i++){
                var curStatus = statuses[i];

                //access bits directly
                var tweetAuthor = curStatus.user.name;
                var tweetTime = curStatus.created_at;

                //iterate hashtags
                var hashtags = curStatus.entities.hashtags;
                for(var k=0;k<hashtags.length;k++){
                        console.log("Hashtag: " + hashtags[k].text);
                }

                //iterate all elements of tweet
                for(var key in curStatus){

                    var attrName = key;
                    var attrValue = curStatus[key];

                    console.log("attribute name: " + attrName);
                    console.log("attribute key: " + attrValue);
                    if(attrName = "text") {
                        //Do something with tweet texts... like: 
                        //document.getElementById("statuses").appendChild(attrValue);
                    }
                }
            }

        }
    }

    xmlhttp.open("GET","index.php",true);
    xmlhttp.send(); 
}

Awesome, this is exactly what I was looking for. Dale is around ... if I see him, I'll say hello!

Ok, I think I know what you are trying to do now. There really isn't an out of the box "for each" like there is in php, which is why a lot of frameworks implement there own (jQuery's $.each()), or make prototypes. But, you may be able to do what you need with the below. You can replace all the console.log() with alert() if you want, but it gets hectic not being in Chrome's dev tools (f12 on most machines). Also, if Dale Musser is still there tell him hello! MIZ

Note