Executing Web Method of ASP.NET Webforms using AngularJS services

Standard

The code written in legacy asp.net web application using webforms c# can be bit tricky to change into angualrJS application, however below angular factory method help to execute a server side WebMethod, method type can be changes to Get or Post as per need:

var app = angular.module('Sample', ['ngRoute']);

app.factory('appFactory', function ($templateCache, $http) {
    var factory = {};
    factory.populateData = function (webMethodUrl, parameterList) {
        return $http({
            method: "POST",
            url: window.location.protocol + '//' + window.location.host + 
                 window.location.pathname + webMethodUrl,
            data: parameterList,
            headers: { "Content-Type": "application/json" },
            responseType: 'json',
            cache: $templateCache
        });
    };
    return factory;
});
Advertisements

4 thoughts on “Executing Web Method of ASP.NET Webforms using AngularJS services

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s