JSONP example

Example of a remote API call via Javascript (jQuery).

by dsummersl

HTML

<html>
<head>
    <script type="text/javascript" src="jquery-1.4.2.js"></script>
    <script type="text/javascript">
        $(function() {
//            $("#getMostRecents").attr("disabled", "true");
            var initialText = $('#apiKey').val();
            $("#apiKey").keyup(function () {
                if (this.value != initialText) {
                    var text = $("#apiKey").val();
                    var len = $("#apiKey").val().length;
                    if (len > 0) {
                        $("#getMostRecents").removeAttr("disabled");
                    }
                    else {
                        $("#getMostRecents").attr("disabled", "true");
                    }
                    initialText = this.value;
                }
            });
            $("#getInteresting").submit(function () {
                var apiKey = $("#apiKey").val();
                $.getJSON(
                    "http://www.flickr.com/services/rest/?jsoncallback=?",
                    {
                        method: "flickr.interestingness.getList",
                        format: "json",
                        api_key: apiKey
                    },
                    function(data) {
                        var photos = data['photos']['photo'];
                        $("#flickrMostRecent *").remove();
                        $("#flickrMostRecent").append("<ul></ul>");
                        var count = 1;
                        var max = 10;
                        $.each(photos,function(i,item) {
                            if (count < max) {
                                $("#flickrMostRecent ul").append("<li>"+ item["title"] +"</li>");
                                count = count + 1;
                            }
                        });
                    });
                return false;
            });
        });
    </script>
</head>
<body>
<h1>Flickr Recents:</h1>

<p>
    First you gotta get an API key. <a...