Wednesday, July 1, 2015

calling web service from angular js

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;

[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.
[System.Web.Script.Services.ScriptService]
public class UserService : System.Web.Services.WebService
{

    public UserService()
    {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public  void GetTalkDetails()
    {
      JavaScriptSerializer js = new JavaScriptSerializer();        
        Person1[] obj = GetPerson();
        Context.Response.Write(js.Serialize(obj));
    }

 
    public  Person1[] GetPerson()
    {
        var pers = new[] {
            new Person1{Id="1",Title="45", FirstName="1",MiddleName="1",LastName="1", Suffix=""},
             new Person1{Id="2",Title="45", FirstName="1",MiddleName="1",LastName="1", Suffix=""},
              new Person1{Id="3",Title="45", FirstName="1",MiddleName="1",LastName="1", Suffix=""},
              new Person1{Id="4",Title="45", FirstName="1",MiddleName="1",LastName="1", Suffix=""},
              new Person1{Id="5",Title="45", FirstName="1",MiddleName="1",LastName="1", Suffix=""},
              new Person1{Id="6",Title="45", FirstName="1",MiddleName="1",LastName="1", Suffix=""}
            };
        return pers;
    }
 
}
public class Person1
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string Suffix { get; set; }
}



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"
    Inherits="Default2" ValidateRequest="false" %>

<!DOCTYPE html>
<html ng-app="serviceConsumer">
<head id="Head1" runat="server">
    <title>consume JSON web service</title>
 <script src="js/angular.js"></script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div ng-controller="questionsController">
            search:<input type="text" ng-model="search" />
            <table>
                <tr ng-repeat="i in questions | filter:search">
                    <td>
                        {{i.QuestionID}}
                    </td>
                    <td>
                        {{i.QuestionText }}
                    </td>
                </tr>
            </table>
             <table class="table table-condensed table-hover">
            <tr>            
                <th>Title</th>
                <th>FirstName</th>
                <th>MiddleName</th>
                   <th>LastName</th>
                <th>IDD</th>
            </tr>
            <tr ng-repeat="talk in pers">          
                <td> &nbsp; {{talk.Title}}</td>
                <td> {{talk.FirstName}}</td>
                <td> {{talk.MiddleName}}</td>
                <td> {{talk.LastName}}</td>
                 <td> {{talk.Id}}</td>
            </tr>
        </table>
        </div>
        <br />
    </div>
    <script>

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

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

            var url = "WebService.asmx/HelloWorld";

            $http.get(url)
                       .success(function (data) {

                           var myjson = JSON.parse(data);

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

                       });

            url = 'Default2.aspx/GetTalkDetails';
          // url = "UserService.asmx/GetTalkDetails";
            $http.get(url)
                      .success(function (data) {
                          $scope.pers = data;

                      });


        })

    </script>
    </form>
</body>
</html>
---------------

Web config File


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

No comments:

Post a Comment