Google+
Showing posts with label Web API. Show all posts
Showing posts with label Web API. Show all posts

Tuesday, April 7, 2020

Solved : Preflight Request CORS error : "The requested resource does not support http method 'OPTIONS'"

In this article we review how to solve the Preflight CORS error "The requested resource does not support http method 'OPTIONS'".
Are you confronted to this error ? :


For several CORS requests, the browser sends a previous request, named "preflight request", before it sends the actual request for the resource.
It is not enought to use the standard CORS (Cross Origin Resource Sharing) on web.config , in order to solve the problem.
Here you can see the web.config settings, usually arranged to solve the problem :







How to solve the Preflight CORS error "The requested resource does not support http method 'OPTIONS'" using Angular7



The issue appears while crossing domains, and relates to POST, PUT, PATCH, and DELETE methods:
here you can see several requests , also from the Angular 7 app, as from POSTMAN :








As said, it is not enought to set the web.config to CORS.
Now,  usually the browser sends a previous request to the request that you are sending, named "preflight request", before it sends the actual request for the resource. This Preflight request is of OPTIONS type.
If the response headers do not allow the required HTTP METHOD, the browser do not send it.

The problem is, the WebApi app has not an action to process the Preflight OPTIONS request :



That is what the error is trying to say us.

So, just write an action for OPTIONS verb, that only responds an "OK", as this :



Now try again. Send a request. The breakpoint will show the OPTIONS request being processed :



Immediately after, the intended request is taken care of:





That solves this issue.
Have a good day :-)

Enjoy Angular.....

      by Carmel Schvartzman

כתב: כרמל שוורצמן

Sunday, February 10, 2019

#16 - How to send PATCH Requests to an OData Web API RESTful service using $http AngularJS

In this article we review how to send PATCH Requests to an OData Web API RESTful service using $http AngularJS,  by developing from scratch in 20 minutes an AngularJS SPA Application that includes a Controller with an $http dependency injection, to send HTTP PATCH Requests to Edit the items into a REST OData web service . This SPA will send also HTTP GET and HTTP POST requests.
The source code for this SPA can be found in the following GitHub repository:

https://github.com/CarmelSoftware/AngularJS_OData_HTTPPATCH/tree/master

This step-by-step tutorial can be learned as a standalone, but also you can see the previous lessons of the series, using the arrows below. This is the Lesson #16 in the "AngularJS: From 0 To 100" articles written for Beginners. This lessons start at Lesson #1   .

<<<<  PREVIOUS LESSON                              NEXT LESSON >>>>


(If you need to follow a short Tutorial about using the "Brackets" open source web Editor , optimized for AngularJS development, you can browse to this post.)

This is a snapshot of the AngularJS App that we'll develop from scratch here, in 20 minutes  :




How to send PATCH Requests to an OData Web API RESTful service using $http AngularJS


While we move forward through this Tutorial, you'll be given the source code to copy-paste to your project. But always you could download the entire AngularJS SPA App from the following GitHub repository, packed in a ZIP file:
https://github.com/CarmelSoftware/AngularJS_OData_HTTPPATCH/archive/master.zip


First, let's add the references to the scripts and styles to use, via CDN(content delivery network), instead of downloading all files to our app. Also, we are defining the HTML5 file as an data-ng-app Angular App:



As you see, we add 2 AngularJS scripts, and 2 Bootstrap CSS3 files. Also, we create directories for "Content" and "Controllers".

(get the source code):

<!doctype html>
<html data-ng-app="OrchidsApp">
<head>
    <title>AngularJS SPA App
    </title>
    <link href="Contents/Style.css" rel="stylesheet" />
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"  />
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css"  />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.7/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.7/angular-route.js"></script>
    <script src="/App/Controllers/SPAControllers.js" type="text/javascript"></script>
</head>


Next, copy-paste the following CSS3 style to your style.css (placed at the "Contents" folder) file:

body {background:rgba(255, 238, 238, 0.5);
}
img {width:99%;height:99%;
}
.select
{
width:100px;
padding:5px 5px 5px 25px;
margin:10px 15px 15px 25px;
font:900 12px Comic Sans MS;
opacity:0.9;
background:#f0f0f0;    
border:5px solid #ddd;    
border-radius: 10px;
box-shadow:10px 10px 2px #c0c0c0;
}
.centered
{
 text-align:center;   
}
.div-table{
  display:table;         
  width:auto;         
  background-color:#eee;
  border-spacing:5px; 
}
.div-table-row{
  display:table-row;
  width:auto;
  clear:both;
}
.div-cell-left{
  float:left; 
  display:table-cell;         
  width:33%;
  height:200px;         
  padding:5px 5px 5px 5px;
}
.div-cell-center{
  float:left; 
  display:table-cell;         
  width:56%; 
  height:200px;         
  padding:5px 5px 5px 5px;
}
.div-cell-right{
  float:left; 
  display:table-cell;         
  width:10%; 
  height:200px;         
  padding:5px 5px 5px 5px;
}
.msg {
font:900 Comic Sans MS;
color:#1b42ae;
}

Next, add a NavBar.html file (again at the Contents folder) containing the Bootstrap NavsBar as follows (adding the Twitter Bootstrap is thoroughly explained in this Bootstrap Tutorial):

<nav class="navbar navbar-default">
    <div class="container-fluid">

        <div class="navbar-header">
           
            <a class="navbar-brand" href="/">Orchids</a>
        </div>


        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="/">Home <span class="sr-only">(current)</span></a></li>
                <li><a href="#/">Orchids SPA</a></li>
                <li><a href="/PDF">Create PDF</a></li>
                <li><a href="/Home">Help</a></li>
                <li><a href="/Home/About">About</a></li>

            </ul>
            <form class="navbar-form navbar-left" role="search">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="Search">
                </div>
                <button type="submit" class="btn btn-default">Submit</button>
            </form>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="/Home">Technologies</a></li>

            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container-fluid -->
</nav>




In the main HTML file, add a <div> bound to an data-ng-view, and a data-ng-include to insert the NavBar HTML5 inside the web page:




(get the source code):
<body class="container">
    <div data-ng-include="" src="'Contents/Navbar.html'"  ></div>
    <div class="jumbotron">
        <h1>Orchids SPA - AngularJS App</h1>
        
    </div>
    <div id="container">

        <div data-ng-view=""></div>

    </div>  
</body>
</html>

This <div> element will be replaced by AngularJS with three Template Views, according to the User's selection ("#/" , "#/add"  and  "#/edit"),  that we're going to design next: a List of Items, a template for Adding a new Item, and another one for Editing .
The first one is for the List of flowers, so create an "/App/Views/OrchidsList.html" file, and paste the following code inside it:

<div class="jumbotron" >  
    <h2>List of my Favorite Orchids</h2>
    <h4 class="msg">{{Message}}</h4>  
</div>  
<div class="jumbotron">    

        <ul  class="list-group">                
            <li data-ng-repeat="Orchid in OrchidsList" class="list-group-item">  
                <div class="div-table" >
                 <div class="div-table-row">    
                     <div class="div-cell-left">
                        <img src="http://carmelwebapi.somee.com/AngularJS/Contents/Images/{{Orchid.MainPicture}}" alt="{{Orchid.Title}}" title="{{Orchid.Title}}"  > 
                     </div> 
                     <div  class="div-cell-center">          
                        <span >{{Orchid.BlogID}}  .  {{Orchid.Title | uppercase}} <br /><br />
                            {{Orchid.Text}}   {{Orchid.DatePosted | date }}
                        </span>  
                    </div>
                     <div class="div-cell-right">
                         <a href="#/edit/{{Orchid.BlogID}}">
                            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                         </a>
                         
                     </div>
                  </div>
                </div>
            </li>             
        </ul> 
        <div class="panel panel-default">      
            <div class="panel-body | centered">
                <button data-ng-click="fnShowOrchids(-1)" class="btn btn-default  btn-lg" ><span class="glyphicon glyphicon-hand-left" aria-hidden="true"></span></button>
                <input type="number" data-ng-model="pageSize" max="4" min="1" value="2" class="select"/>
                <button data-ng-click="fnShowOrchids(1)" class="btn btn-default  btn-lg" ><span class="glyphicon glyphicon-hand-right" aria-hidden="true"></span></button>
            </div> 
        </div>  
    <a href="#/add">Add your Favorite Flowers</a>
</div>




I've remarked the more relevant code in red. The two buttons are for Pagination of the items forward and backwards. The Input "number" is for setting the size of every page while paging. Also, we just display all the flowers by showing all the OrchidsList that we will prepare at the Controller.
We explained using AngularJS collections and the data-ng-repeat in a previous lesson .
There is also a link button to load the "Edit" View Template, using one of the Bootstrap's Glyphicons.
This link sends the ID of the selected item to the Edit Controller.

Next, we're going to code the AngularJS Module, the Routing , and the Controllers.

Create a javascript SPAControllers.js file, and add a Module with a RouteProvider, as follows:





var oOrchidsApp = angular.module('OrchidsApp', ['ngRoute','ngResource']);

oOrchidsApp.config(['$routeProvider', function ($routeProvider) {

    $routeProvider

    .when('/',
        {
            templateUrl: "App/Views/OrchidsList.html",
            controller: "OrchidsAllCtl"
        })
    .when('/add',
        {
            templateUrl: "App/Views/OrchidsAdd.html",
            controller: "OrchidsAddCtl"
        })
     .when('/edit/:id',
        {
            templateUrl: "App/Views/OrchidsEdit.html",
            controller: "OrchidsEditCtl"
        })
   
    .otherwise({ redirectTo: "/" });

}]);



We are using an AngularJS $routeProvider to bind each View with the correspondent Template and  Controller. When the default "/" is required, the user will be faced with the "All" template. If the "/add" page is required, then the "Add" template will be displayed.
The same thing for the "Edit" template.
Remember, this is always the same HTML web page that is browsed here. This is a SPA application: all is done in THE SAME url: there are no reloads of the HTML page!!!
That's why we referenced the angular-route.js javascript at the <head>: to use the $routeProvider at the Module, and enabling an SPA application.

Next we add some Global variables, that is, some data that will be reused by the different Controllers, and we do not want to repeat several times all over the App:






oOrchidsApp.value('msg', { value: '' });

oOrchidsApp.factory('GlobalSvc', [function () {

    var oFlowersPictures = ["haeckel_orchidae.jpg", "Bulbophyllum.jpg", "Cattleya.jpg", "Orchid Calypso.jpg", "Paphiopedilum_concolor.jpg", "Peristeria.jpg", "Phalaenopsis_amboinensis.jpg", "Sobralia.jpg"];
    var sURLDev = 'http://localhost:21435/WebAPI/OrchidsWebAPI/';
    var sURLProd = 'http://CARMELWEBAPI.SOMEE.COM/WebAPI/OrchidsWebAPI/';
    var bIsDevelopmentTest = false;
    var sURL = bIsDevelopmentTest ? sURLDev : sURLProd;

    return {
        getFlowers: function () { return oFlowersPictures; },
        getURL: function () { return sURL; }

    };
}]);

The "value" object will contain a global message which communicates between the different Templates.
The "GlobalSvc" is a Service created by an AngularJS Factory, containing the URLs used all over the Application, and also the list of items to be displayed by the Select list that we'll put in every View Template.
We code two URLs: one for testing purposes and debugging, and one for Deployment.

Next, we create a Resource, that will help us to sending HTTP GET requests for a specific ID of an item:



oOrchidsApp.factory('OrchidsResource', ['GlobalSvc', '$resource',function (GlobalSvc, $resource) {
   
    return $resource(GlobalSvc.getURL() + ":id", { id: "@id" });
}]);


Now we need the code for loading the View ALL Items Template, therefore add to the Module the following Controller:

oOrchidsApp.controller('OrchidsAllCtl', ['GlobalSvc', '$scope', '$http', '$log', 'msg', function (GlobalSvc, $scope, $http, $log, msg) {

    $scope.angularClass = "angular";
    $scope.OrchidsList = [];
    $scope.pageSize = 2;
    var iCurrentPage = -1; 
     

    $scope.fnShowOrchids = function (direction) {

        iCurrentPage = iCurrentPage + direction;
        iCurrentPage = iCurrentPage >= 0 ? iCurrentPage : 0;
        
        var sURL = GlobalSvc.getURL() +
            "?$skip=" +
            iCurrentPage * $scope.pageSize
            + "&$top=" +
            $scope.pageSize;


        $http.get(sURL).success(function (response) {

            $scope.OrchidsList = response;
            $log.info("OK");

        },function (err) { $log.error(err) })
        
        $scope.Message = "";
        
    }

    $scope.fnShowOrchids(1);
    $scope.Message = msg.value;

}
]);






This HTTP GET Ajax request code is thoroughly explained in the Lesson #14. Here we just get the URL from the "GlobalSvc" Service, append to it the OData code for Paging ("$skip=" + "$top="),  and send an HTTP GET REST request via the $http Service.
The "direction" function argument, as explained in a previous tutorial, is for paging backwards and forward.
Notice that we made Dependency Injections for three services : $scope, to get the variables, $http for sending HTTP REST requests to the web server, and $log for logging and making easy to debug our app.
In case you don't have an OData RESTful web service working on your environment, i developed and deployed one that you can use. It can be found at this URL:

http://carmelwebapi.somee.com/WebAPI/OrchidsWebAPI

You can use freely use it: an example of using this OData Web API:

http://carmelwebapi.somee.com/WebAPI/OrchidsWebAPI/?$skip=2&$top=3

If you already have an OData service working, just replace the URLs at the Service.

We're going to fetch the data from an OData RESTful  service, by using the Ajax Service called $http in Angular. This service provide all kinds of HTTP functionality, like sending  HTTP POST, PATCH, HTTP PUT or DELETE requests. Next we will use both the HTTP GET, and the HTTP POST and PATCH verbs.
The documentation for the $http service can be seen at the Angular official web site:

How to Debug AngularJS Apps with the free Batarang Debugger        17




Now let's design the AngularJS Template View for adding a new item to the Orchids collection.
The "Add" Template will show as follows:


<div class="container">
<div class="jumbotron">
    <div class="" >    
        <h2>Add your Favorite Orchid</h2>  
    </div> 
        <form name="addOrchid" class=""
            data-ng-submit="fnAdd()">
            <input type="text" class="form-control"
            placeholder="Title"
            data-ng-model="Orchid.Title"
            required>
            <input type="text" class="form-control"
            placeholder="Text"
            data-ng-model="Orchid.Text"
            required>
            <select data-ng-model="Orchid.MainPicture"  title="Select a Picture" data-ng-options="Img for Img in Flowers" class="form-control"></select>
            <input type="submit" class="btn btn-default  btn-lg"
            value="Add"
            data-ng-disabled="addOrchid.$invalid">
            <span>{{fnShowMsg()}}</span>
        </form>
    <a href="#/">See All Flowers</a>
</div>
</div>





At the Module, add a new Controller to add a ITEM capabilities of our SPA:




(get the source code):

oOrchidsApp.controller('OrchidsAddCtl',
    ['GlobalSvc', '$http', '$scope', '$location', '$log', 'msg',
        function (GlobalSvc, $http, $scope, $location, $log, msg) {
            msg.value = "";
            $scope.Flowers = GlobalSvc.getFlowers();
            
            $scope.fnAdd = function () {

                var oFlower = { "Title": $scope.Orchid.Title, "Text": $scope.Orchid.Text, 
                               "MainPicture": $scope.Orchid.MainPicture };    

                $http({
                    url: GlobalSvc.getURL(),
                    method: "POST",
                    data: oFlower, 
                    headers: { 'Content-Type': 'application/json' }
                }).success(function (data, status, headers, config) {
                    msg.value = "New Orchid saved";
                    $scope.IsSaved = true;
                }).error(function (err) {
                     $log.error(err);
                });                
            }

            $scope.fnShowMsg = function () { return msg.value; }
            
}
]);

Finally, we'll create the "Edit" View Template for our SPA. Add a new HTML file called "" and type the following markup:


<div class="container">
<div class="jumbotron">
    <div class="" >    
        <h2>Edit your Favorite Orchid</h2>  
    </div> 
        <form name="editOrchid" class=""
            data-ng-submit="fnEdit()">
            <input type="text" class="form-control"
            placeholder="Title"
            data-ng-model="Orchid.Title"
            required>
            <input type="text" class="form-control"
            placeholder="Text"
            data-ng-model="Orchid.Text"
            required>
            <select data-ng-model="Orchid.MainPicture"  title="Select a Picture" data-ng-options="Img for Img in Flowers" class="form-control"></select>
            <input type="submit" class="btn btn-default  btn-lg"
            value="Update"
            data-ng-disabled="editOrchid.$invalid">
            <span>{{fnShowMsg()}}</span>
        </form>
    <a href="#/">See All Flowers</a>
    
</div>
    </div>


Then go back to the  javascript Module and append another Controller as follows:


oOrchidsApp.controller('OrchidsEditCtl',
    ['OrchidsResource', 'GlobalSvc', '$http', '$routeParams', '$scope', '$location', '$log', 'msg',
        function (OrchidsResource, GlobalSvc, $http, $routeParams, $scope, $location, $log, msg) {

    msg.value = "";
    $scope.Flowers = GlobalSvc.getFlowers();
    $scope.Orchid = OrchidsResource.get({ id: $routeParams.id });

    $scope.fnEdit = function () {
                
        var oFlower = { "BlogId": $routeParams.id , "Title": $scope.Orchid.Title, 
                       "Text": $scope.Orchid.Text, "MainPicture": $scope.Orchid.MainPicture };
        
        $http({
            url: GlobalSvc.getURL() + $routeParams.id,
            method: "PATCH",
            data: oFlower,
            headers: { 'Content-Type': 'application/json' }

        }).success(function (data) { msg.value = "Orchid successfully updated"; }).error(function (err) { });

        
    }
    
    $scope.fnShowMsg = function () { return msg.value; }
}
]);

Here are some new things that will call your attention: the $routeParams.id allows us to get the ID of the specific item to be edited.
The get() method of the OrchidsResource just send an HTTP GET request to get THIS specific item that we're going to edit.
Then we use $http to send an HTTP PATCH request, and if the response is OK ("success"), we change the "msg" value to output some feedback to the user. Also, this "msg" variable is Global, and that will allows us to show a message also at the List View Template, although it has an other different $scope at all.

Save and run the SPA:




Now you can click the "Add" link to be prompted with the "Add" template, which will look this way:




Here you can add a new item. This is how looks the list of pictures at the drop down list:




Save a flower, to see how it works:



Now you will see the "New orchid saved" message (provided that the web service is working properly).
Return to the "See all flowers" View (because this is a SPA Application, we're actually browsing to the SAME html web page):


Here we can see the new item that we added to the collection, and clicking the "Edit" icon, we'll edit it:

As you can see, the URL contains the ID of the item being edited. Make some changes, and click the "Update" button, to send an HTTP PATCH request to the OData REST service:


If you get no response, check at the Developer's Tools (F12) in the "Network" tab, for the response status. If there is some error , refer to this HTTP Error Tutorial.
After you updated your item, take a look at it on the items List:


Notice the "Msg" that we display here , in the "All" View.

Remember to widely using the $log functionality in your SPA, to send to yourself messages with some feedback from your AngularJS app.

That's All!!! You have started sending HTTP PATCH Requests to an OData Web API RESTful service,  in your own SPA application, using the AngularJS Dependency Injection for the $http, $log, and $routeParameters Services. In the next article we will continue to learn Step by Step about  making HTTP DELETE requests to an odata web api restful service using the $http  angularjs . 
Enjoy AngularJS.....

      by Carmel Schvartzman


<<<<  PREVIOUS LESSON                        NEXT LESSON >>>>



כתב: כרמל שוורצמן

Thursday, March 31, 2016

#14 - Make GET Requests with pagination to OData Web API RESTful service using $http AngularJS

In this article we review how to Make GET Requests to an OData Web API RESTful service using $http AngularJS,  by developing in 15 minutes an Angular Application that includes a Controller with an $http service dependency, to fetch data from a REST OData web service , loading a paginated list of products.
You can learn this step-by-step tutorial as a standalone, or you can see the previous lessons of the series. This is the Lesson #14 in the "AngularJS: From 0 To 100" articles written for Beginners. This lessons start at Lesson #1   .
This App can be downloaded from the following GitHub repository:
https://github.com/CarmelSoftware/AngularJS_OData_HTTPGET


<<<<  PREVIOUS LESSON               NEXT LESSON >>>>


In this tutorial we're using the "Brackets" open source web Editor , which supports AngularJS Apps, and you can read more about it in this post.

This is the AngularJS App that we'll develop here from scratch :

How to Debug AngularJS Apps with the free Batarang Debugger      14


Make GET Requests with pagination to OData Web API RESTful service using $http AngularJS


You can download this entire App from the following URL:
https://github.com/CarmelSoftware/AngularJS_OData_HTTPGET/archive/master.zip

First, let's add the references to the AngularJS javascript framework using CDN, and defining the HTML5 file as an data-ng-app Angular App:





How to Debug AngularJS Apps with the free Batarang Debugger



(get the source code):

<!doctype html>
<html data-ng-app="OrchidsApp">
<head>
<title>
    AngularJS HTTP GET
</title>
     
    <link href="Style.css" rel="stylesheet" type="text/css"  />
    <script  src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.7/angular.min.js"></script>
    <script src="Controllers.js" type="text/javascript"></script>
</head>


Next, copy-paste the following CSS3 style to your style.css file:

body {background:rgba(255, 238, 238, 0.5);
}
img {width:100px;height:100px;
}
div[id^=controls] , input[type=number]
{
padding:5px 5px 5px 25px;
margin:10px 15px 15px 25px;
font:900 12px Comic Sans MS;
opacity:0.9;
background:#fed;    
border:10px solid #ddd;    
border-radius: 10px;
box-shadow:10px 10px 2px #c0c0c0;
}
button
{
padding:5px 5px 5px 5px;
margin:10px 15px 15px 15px;
width:120px;
text-align: center;
background:#ddd; 
font:900 14px Comic Sans MS;
opacity:0.9;
border:1px solid #c0c0c0;    
border-radius: 10px;
box-shadow:10px 10px 2px #c0c0c0;
}
ul
{
list-style:none;    
}
.centered
{
 text-align:center;   
}
.angular
{
padding:5px 5px 5px 5px;
margin:25px 15px 15px 25px;
width:350px;
text-align:center;
background:#fee; 
font:900 14px Comic Sans MS;
opacity:0.9;
border:1px solid #c0c0c0;    
border-radius: 10px;
box-shadow:10px 10px 2px #faf;
}
.title {
    width: 90%;
    border: solid 1px #fea;
}


Add a <div> bound to an data-ng-controller:

How to Debug AngularJS Apps with the free Batarang Debugger       1



(get the source code):
<body class="centered">
<div class="angular | title" >                  
   <h1>Orchids AngularJS App</h1>     
    <h2>List of my Favorite Orchids</h2>  
</div>      
    <div id="controls1" data-ng-controller="OrchidsCtl"> 


Create a javascript Controller.js file, and add a module with a Controller:

How to Debug AngularJS Apps with the free Batarang Debugger      2

Notice we did a Dependency Injection for three services : $scope, to work with variables, $http for sending HTTP requests to the web server, and $log for making it easy to debug our app.

(get the source code):

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

oOrchidsApp.controller('OrchidsCtl', ['$scope', '$http', '$log', function ($scope, $http, $log) {

    $scope.angularClass = "angular";
    $scope.OrchidsList = [];


At the HTML5 file, add a list and use the data-ng-repeat directive, for displaying the "Orchid" items when loaded from the OData web service:

How to Debug AngularJS Apps with the free Batarang Debugger      3



(get the source code):

<ul >                
            <li data-ng-repeat="Orchid in OrchidsList">   
                 <div data-ng-class="angularClass" >                         
                        
                 <img src="Images/{{Orchid.MainPicture}}" alt="{{Orchid.Title}}"  > <br />           
                    <span >{{Orchid.BlogID}}  .  {{Orchid.Title | uppercase}} <br />
                            {{Orchid.Text}}   {{Orchid.DatePosted | date }}
                    </span>  
                 </div>
            </li>             
        </ul>        


We thoroughly  explained using AngularJS collections in this previous lesson .
The difference is, now we're going to fetch the data from an OData RESTful  web service, using an Ajax enabled AngularJS Service called $http. This service provide also another kinds of HTTP requests like POST, PUT etc. Here we only need to use the HTTP GET verb.
More info about the $http AngularJS service can be found at the Angular official web site:

How to Debug AngularJS Apps with the free Batarang Debugger        17


So let's use the $http get() method service as follows:

How to Debug AngularJS Apps with the free Batarang Debugger      4



(get the source code):


$scope.fnShowOrchids = function (direction) {

       
        $http.get(sURL).success(function (response) {

            $scope.OrchidsList = response;
            $log.info("OK");

        },

         function (err) { $log.error(err) })
    }

You can see that we concatenate this get() method with another "success" method, a callback which  gets two functions as parameters: the first is the callback called when everything's fine, and the second is for when something went wrong.
Inside the method, we just load the "OrchidsList" with the data returned by the HTTP response.
Now , we need a REST OData web service, which in my case is the following:

How to Debug AngularJS Apps with the free Batarang Debugger      10

In case that you don't have an OData RESTful web service on your project, i developed one that you can use, in this URL:

http://carmelwebapi.somee.com/WebAPI/OrchidsWebAPI

This OData RESTful service is exactly the same as described in this tutorial. You can use it at your own.
An example of using this OData Web API:
http://carmelwebapi.somee.com/WebAPI/OrchidsWebAPI/?$skip=2&$top=3


Now , about Pagination. We don't want all the data to be displayed on the web page, so we use the built in OData pagination capabilities. The OData service supports a "skip" and a "top" parameters, for example, this snapshot shows skipping 2 orchids blog posts, and displaying the top 3 items:

How to Debug AngularJS Apps with the free Batarang Debugger    11

In other words, in this example the Page has 3 items, and we just skipped 2 other ones.
To produce the pagination, we add two buttons that call a Controller's method "fnShowOrchids", telling it to display 1 previous page  (-1) and 1 next page (1):



How to Debug AngularJS Apps with the free Batarang Debugger        5



(get the source code):

<div class="centered"> 
            
            <button data-ng-click="fnShowOrchids(-1)"><<<<</button>
           
            <button data-ng-click="fnShowOrchids(1)">>>>></button>
            
        </div> 


At the Controller, we should add the Page size and the Current page variables, and calculate which page claim from the web server:

How to Debug AngularJS Apps with the free Batarang Debugger    6



(get the source code):

 $scope.pageSize = 2;

 var iCurrentPage = -1;

$scope.fnShowOrchids = function (direction) {

        iCurrentPage = iCurrentPage + direction;
        iCurrentPage = iCurrentPage >= 0 ? iCurrentPage : 0;
        
        var sURL = "http://localhost:21435/WebAPI/OrchidsWebAPI/" +
            "?$skip=" +
            iCurrentPage * $scope.pageSize
            + "&$top=" + 
            $scope.pageSize;


        $http.get(sURL).success(function (response) {

            $scope.OrchidsList = response;
            $log.info("OK");

        },

         function (err) { $log.error(err) })
    }

We build the URL , and send the current Page and also the its size :

How to Debug AngularJS Apps with the free Batarang Debugger    7




We made the page size an $scope variable, a part of the Model, in order to allow the user to modify its size, using the following HTML5 input number element:

How to Debug AngularJS Apps with the free Batarang Debugger       8



(get the source code):

 <input type="number" data-ng-model="pageSize" max="4" min="1" value="2" />

Then the $scope.pageSize allows us to calculate the "skip" and "top" values:

How to Debug AngularJS Apps with the free Batarang Debugger      9



We're done with the code. Now, run the app to get this web page:




How to Debug AngularJS Apps with the free Batarang Debugger     12

Click the NEXT (>>>) button and see the data fetched from the OData service:


How to Debug AngularJS Apps with the free Batarang Debugger       13


The page size is "2" by default, as we instantiated it at the Controller:

How to Debug AngularJS Apps with the free Batarang Debugger      14

Now, let's change the page size:


How to Debug AngularJS Apps with the free Batarang Debugger       15


As you see, now the page size is 1:

How to Debug AngularJS Apps with the free Batarang Debugger       16

Remember to use the $log functionality to send to yourself messages , easing to debug the AngularJS app.

That's All!!! You have begun making GET Requests with pagination to OData Web API RESTful service using $http AngularJS with Dependency Injection . In the next article we will continue to learn Step by Step about  making post requests to an odata web api restful service using the $http  angularjs . 
Enjoy AngularJS.....

      by Carmel Schvartzman


<<<<  PREVIOUS LESSON             NEXT LESSON >>>>



כתב: כרמל שוורצמן