Click here to Skip to main content
15,798,200 members
Articles / Web Development / ASP.NET

Consuming JSON ASMX Web Services with AngularJS: A Beginners "Experience" !

Rate me:
Please Sign up or sign in to vote.
4.97/5 (20 votes)
7 Jan 2015CPOL3 min read 122.1K   2.6K   42   21
A step by step tutorial showing how to consume an ASMX Service from an application using AngularJS, highlighting common pitfalls.


This article will explain how to consume an ASMX service using AngularJS. The scope of this article is limited to illustrate a simple example of creating and consuming JSON ASMX services using AngularJS. Pitfalls personally faced by the author, being new to both Web Services and AngularJS, are highlighted in this article to spare our fellow beginners the "start-up" agony. It is to be noted that ASMX web service is now a legacy technology. Recently, more advanced technologies are used to build Web Services with better performance.

To build our sample application, we have used AngularJS version 1.2.16. This article demonstrates how AngularJS will retrieve a list of Questions from a database. The ASMX web service is hosted in the same web application. In this example, I used ASPX Webform and an MSAccess backend, but you can use HTML page instead of the Webform and, of course, any database.

For calling the service, we need to use the $http.get() method of AngularJS, which sends a Request to the server. The ASMX service then sends a response in JSON format, that will be parsed and consumed in AngularJS.


JSON (JavaScript Object Notation) is a lightweight, language independent, data-interchange format. JSON is syntax for storing and exchanging text information. JSON is smaller than XML, faster and easier to parse. [1,2]

A Web Service is a method of communication between two electronic devices over any network. It is designed to allow applications built using different technologies to communicate with each other using international standards.

AngularJS "lets you extend HTML vocabulary for your application. The resulting environment is extraordinarily expressive, readable, and quick to develop." [3]

Using the Code: Step by Step

The business logic of the sample application is quite simple. The QuestionID and QuestionText were stored in Questions table in Quiz.mdb. When the service Web Method is invoked, the service connects to the database, retrieves a list of questions as a DataTable, uses System.Web.Script.Serialization.JavaScriptSerializer() method to Convert DataTable to JSON String, and finally, supplies a list of Questions to be returned in JSON format.

Step (1) Create a New Web Application

Create a new Web Site, then create a new ASMX Web Service and a new WebForm or new HTML page.

Step (2) Create a Simple Database

In this example, I used MSAccess. You can use any database, of course. you will only need to change the connection string.

Image 1

Step (3) Write the Code for your Business Logic

// in this sample, and as described in the article, 
// this is a simple method that will connect to the database, 
// execute an SQL command and return data in form of a DataTable 
public DataTable GetDataTable()
        DataTable dataTable = new DataTable();
        using (OleDbConnection conn = new OleDbConnection
        ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|Quiz.mdb"))
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from Questions";

            cmd.CommandType = CommandType.Text;

            if (conn.State != ConnectionState.Open)

            OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        return dataTable;
//once the datatable is ready, it has to be converted to JSON to be consumable with AngularJS, 
//the following function uses Javascript serializer to do the conversion

    public String ConvertDataTableTojSonString(DataTable dataTable)
        System.Web.Script.Serialization.JavaScriptSerializer serializer =
               new System.Web.Script.Serialization.JavaScriptSerializer();

        List<Dictionary<String, Object>> tableRows = new List<Dictionary<String, Object>>();

        Dictionary<String, Object> row;

        foreach (DataRow dr in dataTable.Rows)
            row = new Dictionary<String, Object>();
            foreach (DataColumn col in dataTable.Columns)
                row.Add(col.ColumnName, dr[col]);
        return serializer.Serialize(tableRows);

Step (4) Setup your [WebMethod]

The definition of the Web Method makes it enabled for invoking. Also setup the Response.ContentType to be in JSON format.

[WebService(Namespace = "AnyNameSpace")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, 
// uncomment the following line. 
public class WebService : System.Web.Services.WebService

// Business Logic Code goes here

[WebMethod] // by writing this line, you tell your service to enable this method / function 
  //for public consumption. If you remove this line, the method can not be invoked.
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public void HelloWorld()
        JavaScriptSerializer js = new JavaScriptSerializer();
        Context.Response.ContentType = "application/json";
        HelloWorldData data = new HelloWorldData();
        data.Message = ConvertDataTableTojSonString(GetDataTable());

    public class HelloWorldData
        public String Message;

Step (5) Test your Web Service

Run the Code and Click "Invoke". If you have done everything properly, you should see a message like this:

Image 2

It is important to note that you have to use the data.Message not only data in HelloWorld()

Image 3

If, you use data not data.Message you will not get an error (initially), but you will experience a run time error because AngularJS will fail to parse it.

Step (6) Define the Configuration in web.config

Define the webServices protocols under the section <system.web>.

        <add name="HttpGet"/>
        <add name="HttpPost"/>

Failing to configure the webServices section properly might lead to the following error:

Image 4

Step (7) Prepare Your WebForm (or HTML Page)

The Directive ng-app is used to "auto-bootstrap" an AngularJS application. The questionsController will get the data from the $scope, which in turn gets its data from $http.get() function and parses the message content using JSON.parse() method. Pitfalls include the wrong placing of directives and the improper parsing of JSON.

<html ng-app="serviceConsumer">
<head runat="server">
    <title>consume JSON web service</title>
    <script src="js/angular.js"></script>
    <form id="form1" runat="server">
        <div ng-controller="questionsController">
            search:<input type="text" ng-model="search" />
                <tr ng-repeat="i in questions | filter:search">
                        {{i.QuestionText }}
        <br />

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

        app.controller('questionsController', function ($scope, $http) {

            var url = "WebService.asmx/HelloWorld";

                       .success(function (data) {

                           var myjson = JSON.parse(data);

                           $scope.questions = JSON.parse(myjson);


Step (8) Running Your Application on a different Domain

If you run your service consuming application on a domain different from the domain providing the webservice, you might face an "Access-Control" error.

Image 5

In this case, adding the following section to Web.config will solve this issue.

        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type"/>




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Team Leader
Egypt Egypt
Clinical Pharmacy AND Programming were, and still are, my life's passion. Children suffering from cancer has been my cause since 1998. I chose to "focus" on Children with Brain Tumors, as it is -till this day- one of the most challenging pediatric malignancies. Throughout my professional career, i learnt a lot of stuff to help me help children with cancer. Working with those under privileged kids also required me to have a deeper knowledge of Pediatric Oncology, Patient Education and cancer supportive care. I got training in different fields -not only- Clinical Pharmacy, but also Fundraising, Clinical Informatics and Web Development !

Comments and Discussions

QuestionWOW Pin
EmadAldin Baybars14-Nov-17 22:26
EmadAldin Baybars14-Nov-17 22:26 
AnswerRe: WOW Pin
MohamedKamalPharm15-Mar-18 2:56
MohamedKamalPharm15-Mar-18 2:56 
GeneralNice Work Pin
arpit bhatnagar30-May-16 11:24
arpit bhatnagar30-May-16 11:24 
GeneralRe: Nice Work Pin
MohamedKamalPharm30-May-16 20:52
MohamedKamalPharm30-May-16 20:52 
QuestionWorking fine Pin
Syed Naqiul Hasan29-Nov-15 23:45
Syed Naqiul Hasan29-Nov-15 23:45 
QuestionAdd a http Post Pin
Fagro Vizcaíno Salcedo30-Jun-15 9:48
Fagro Vizcaíno Salcedo30-Jun-15 9:48 
QuestionGreat job sir. Pin
samip biswas18-Mar-15 3:41
samip biswas18-Mar-15 3:41 
AnswerRe: Great job sir. Pin
MohamedKamalPharm18-Mar-15 4:13
MohamedKamalPharm18-Mar-15 4:13 
QuestionGreate job! Pin
rasm200014-Jan-15 21:36
rasm200014-Jan-15 21:36 
AnswerRe: Greate job! Pin
MohamedKamalPharm14-Jan-15 22:03
MohamedKamalPharm14-Jan-15 22:03 
GeneralGreat article Pin
willemvg14-Jan-15 4:15
willemvg14-Jan-15 4:15 
GeneralRe: Great article Pin
MohamedKamalPharm14-Jan-15 12:02
MohamedKamalPharm14-Jan-15 12:02 
QuestionGreat! Pin
Armando Airo'8-Jan-15 1:36
Armando Airo'8-Jan-15 1:36 
AnswerRe: Great! Pin
MohamedKamalPharm8-Jan-15 3:35
MohamedKamalPharm8-Jan-15 3:35 
QuestionHandy script, how about resurrect FROM JSON? Pin
devvvy7-Jan-15 22:08
devvvy7-Jan-15 22:08 
AnswerRe: Handy script, how about resurrect FROM JSON? Pin
MohamedKamalPharm8-Jan-15 3:36
MohamedKamalPharm8-Jan-15 3:36 
QuestionAwesome article sir! Question what if the webservice has a parameter? Pin
CcoVillar19-Nov-14 20:43
CcoVillar19-Nov-14 20:43 
AnswerRe: Awesome article sir! Question what if the webservice has a parameter? Pin
CcoVillar23-Nov-14 14:51
CcoVillar23-Nov-14 14:51 
GeneralRe: Awesome article sir! Question what if the webservice has a parameter? Pin
MohamedKamalPharm23-Nov-14 19:37
MohamedKamalPharm23-Nov-14 19:37 
GeneralNice Article Pin
Jagbir Saini10-Oct-14 13:17
professionalJagbir Saini10-Oct-14 13:17 
GeneralRe: Nice Article Pin
MohamedKamalPharm12-Oct-14 21:56
MohamedKamalPharm12-Oct-14 21:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.