enzian
enzian

Reputation: 1731

Javascript get content of URL

If I enter this link, I get an array of search suggestions for the word 'sun' in a textfile 'f.txt'

http://suggestqueries.google.com/complete/search?client=firefox&q=sun

How can I get this into an array in Javascript using only this URL?

Upvotes: 0

Views: 12567

Answers (2)

function httpGet(theUrl)
{
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            return xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", theUrl, false );
    xmlhttp.send();    
}

Upvotes: 0

Overv
Overv

Reputation: 8529

You can make a request using AJAX and then parse the result into a JavaScript object using JSON.parse:

var req = new XMLHttpRequest();
req.onreadystatechange = function() {
    if (req.readyState === 4) {
        var response = req.responseText;
        var json = JSON.parse(response);

        console.log(json)
    }
};

req.open('GET', 'http://suggestqueries.google.com/complete/search?client=firefox&q=sun');
req.send(null);

Upvotes: 3

Related Questions