Saturday, July 25, 2015

AngularJS $http.get Ajax calls

In the post AngularJS Ajax using $http we saw that we can perform Ajax calls to the server in AngularJS using $http service, and we saw that $http supports Get, Post, Put and Delete operations. In this post we shall see on how to make a $http.get Ajax call in AngularJS

The following example shows how to make a $http.get call to an Asp.Net Web API Controller method.
WebAPI Get Method
    public class PackageController : ApiController
    {
        PkgEntities objDBContext = new PkgEntities();
        //
[HttpGet]
public dynamic Get()
{
var lstPackages = (from Packages in objDBContext.Packages
orderby Packages.PackageId
select new { Packages.PackageId, Packages.Name, Packages.Description, Packages.PackageSQL, Packages.Status });
//
return lstPackages;
}
}

AngularJS call to WebAPI Get method
        $http.get(“http://localhost:7630/api/Package")
            .then(function (results) {
                //Success;
                try {
                    scope.packageList = results.data;
                }
                catch (err) {
                    alert(err.message);
                }
            }, function (results) {
                //error
                alert("Error: " + results.data + "; "
                                      + results.status);
            })

The $http.get method calls the server side WebAPI Get() method, collects the response and stores it in the  packageList property of the $scope object.

Search Flipkart Products:
Flipkart.com

No comments: