Json – Example JSON HTTP request for twitter api


I want to make a request to the twitter api. This is the example provided in the documentation (https://dev.twitter.com/docs/api/1/get/search):



There is no example request on the documentation. How would the request for this url be including an alert with data response?

Best Solution

Look if this helps, I made an example for you:

Basically the HTML code contains 2 inputs. one for the button and one for the query string.

   <div style="padding: 20px;">
        <input id="query" type="text" value="blue angels" />
        <input id="submit" type="button" value="Search" />
    <div id="tweets" style="padding: 20px;">
        Tweets will go here.

After pressing the search button, you'll send a request to twitter asking for 5 results (rpp) containing the query string.

Here's the javascript for this page:

function searchTwitter(query) {
        url: 'http://search.twitter.com/search.json?' + jQuery.param(query),
        dataType: 'jsonp',
        success: function(data) {
            var tweets = $('#tweets');
            for (res in data['results']) {
                tweets.append('<div>' + data['results'][res]['from_user'] + ' wrote: <p>' + data['results'][res]['text'] + '</p></div><br />');

$(document).ready(function() {
    $('#submit').click(function() {
        var params = {
            q: $('#query').val(),
            rpp: 5
        // alert(jQuery.param(params));

The trick is the jQuery.param() function that you'll pass the params for the search/request

See it running here:


Related Question