Angular: JSONP example

http://angularjs.org/

by supercobra

HTML

<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-controller="MyCtrl">
     <h1>Angular JSONP example</h1>

     <h2>HTTP call settings</h2>

    <li>Method: {{method}}
        <li>URL: {{url}}
            <br>
            <button ng-click="fetch()">fetch</button>
            <hr/>
             <h3>HTTP call result</h3>

            <li>HTTP response status: {{status}}</li>
            <li>HTTP response data: {{data}}</li>
</div>

JavaScript

var myApp = angular.module('myApp', []);

function MyCtrl($scope, $http, $templateCache) {


    $scope.method = 'JSONP';
    $scope.url = 'http://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero';

    $scope.fetch = function () {
        $scope.code = null;
        $scope.response = null;

        $http({
            method: $scope.method,
            url: $scope.url,
            cache: $templateCache
        }).
        success(function (data, status) {
            $scope.status = status;
            $scope.data = data;
        }).
        error(function (data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;
        });
    };

}