0

I am consuming Wcf Rest Service into Angular JS Application. I am trying to retrieve a single record from Sql database though Wcf Rest Service. The Wcf Service able to post the to Angular JS Application but When i click the submit button in site i do not found the record in Angular js Application .I checked in Google Chrome Network tab ,there I found the that particular record.

Here is the Interface .

[OperationContract]
    [WebInvoke(Method = "GET",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "/GetCustomers/{Account_Holder_Last_Name}")]
    string GetCustomers(string Account_Holder_Last_Name);

Here is Implementation of Interface .

public string GetCustomers(string Account_Holder_Last_Name)
    {

        List<object> customers = new List<object>();
        string sql = "SELECT * FROM Current_Account_Holder_Details WHERE Account_Holder_Last_Name =@Account_Holder_Last_Name";
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand(sql))
            {
                cmd.Parameters.AddWithValue("@Account_Holder_Last_Name", Account_Holder_Last_Name);
                cmd.Connection = conn;
                conn.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    if (sdr.HasRows)
                    {
                        while (sdr.Read())
                        {

                            customers.Add(new
                            {
                                Tittle = sdr["Tittle"],
                                Account_Holder_First_Name = sdr["Account_Holder_First_Name"],
                                Account_Holder_Last_Name = sdr["Account_Holder_Last_Name"],
                                Account_Holder_DOB = sdr["Account_Holder_DOB"],
                                Account_Holder_House_No = sdr["Account_Holder_House_No"],
                                Account_Holder_Street_Name = sdr["Account_Holder_Street_Name"],
                                Account_Holder_Post_Code = sdr["Account_Holder_Post_Code"],

                                Account_Holder_Occupation = sdr["Account_Holder_Occupation"],
                                Account_Number = sdr["Account_Number"]



                            });
                        }

                    }

                }
                conn.Close();
            }

            return (new JavaScriptSerializer().Serialize(customers));
        }

    }

Here is the HTML Code with Angular JS Script code .

@{
    Layout = null;
}

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', [])
        app.controller('MyController', function ($scope, $http, $window) {
            $scope.IsVisible = false;
            $scope.Search = function () {
                var post = $http({
                    method: "GET",
                    url: "http://localhost:52098/HalifaxIISService.svc/GetCustomers/" + encodeURIComponent($scope.Account_Holder_Last_Name),
                    dataType: 'json',
                    headers: {
                        'Accept': 'application/json, text/javascript, */*; q=0.01',
                        'Content-Type': 'application/json; charset=utf-8'
                    }
                });

                post.success(function (data, status) {
                    $scope.Customers = eval(data.d);
                    $scope.IsVisible = true;
                },
                    function (err) {
                        console.log("Some Error Occured." + err);
                    }
                );

                post.error(function (data, status) {
                    $window.alert(data.Message);
                });
            }
        });
    </script>
    <div ng-app="MyApp" ng-controller="MyController">
        Name:
        <input type="text" ng-model="Account_Holder_Last_Name" />
        <input type="button" value="Submit" ng-click="Search()" />
        <hr />
        <table cellpadding="0" cellspacing="0" ng-show="IsVisible">
            <tr style="height: 30px; background-color: skyblue; color: maroon;">
                <th> Tittle</th>
                <th>First Name</th>
                <th> Last Name</th>
                <th>  DOB </th>
                <th> House No</th>
                <th> Street Name</th>
                <th>Post Code</th>
                <th> Occupation</th>
                <th>Account Number</th>


            </tr>
            <tbody ng-repeat="m in Customers">
                <tr>
                    <td>{{m.Tittle}}</td>
                    <td>{{m.Account_Holder_First_Name}}</td>
                    <td>{{m.Account_Holder_Last_Name}}</td>

                    <td>{{m.Account_Holder_DOB}}</td>
                    <td>{{m.Account_Holder_House_No}}</td>
                    <td>{{m.Account_Holder_Street_Name}}</td>
                    <td>{{m.Account_Holder_Post_Code}}</td>

                    <td>{{m.Account_Holder_Occupation}}</td>
                    <td>{{m.Account_Number}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

Here is the Result when i run the application on Network Tab .. enter image description here

7
  • Can you include the response in your post as well? Commented Dec 11, 2017 at 20:05
  • You can check the screen shot Commented Dec 11, 2017 at 20:07
  • For security reasons, it is safer to use JSON.parse instead of eval. Commented Dec 11, 2017 at 21:15
  • Ok. Thanks what other changes I have make Commented Dec 11, 2017 at 21:17
  • Use console.log statements to debug the code. Commented Dec 11, 2017 at 21:26

2 Answers 2

0

I haven't really set this up in my local. But what I suggested is just a simple change as shown below, just adding $scope.Customers = []; at the top.

    <script type="text/javascript">
    var app = angular.module('MyApp', [])
    app.controller('MyController', function ($scope, $http, $window) {
        $scope.IsVisible = false;
        $scope.Customers = [];
        $scope.Search = function () {
            var post = $http({
                method: "GET",
                url: "http://localhost:52098/HalifaxIISService.svc/GetCustomers/" + encodeURIComponent($scope.Account_Holder_Last_Name),
                dataType: 'json',
                headers: {
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Content-Type': 'application/json; charset=utf-8'
                }
            });

            post.success(function (data, status) {
                $scope.Customers = eval(data.d);
                $scope.IsVisible = true;
            },
                function (err) {
                    console.log("Some Error Occured." + err);
                }
            );

            post.error(function (data, status) {
                $window.alert(data.Message);
            });
        }
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand correctly - the service is returning data, but the UI is not rendering it.

I don't see "Customers" object defined. Also you can add a "debugger" statement in the "post.success" and "post.error" methods to see where exactly the issue is coming up. It shows up the exceptions clearly.

Just add the debugger statement and F12 in your browser. Hope that helps you to figure out the problem.

Regards
Neelima

4 Comments

on post.success function i defined the customers object .but if you say its not defined where i have to make the changes
Its says local host undefined
Where I have to define??
I guess u will have to define it outside the function as well in order to access it in your HTML code. Under $scope.IsVisible = false, u can define $scope.Customers = [] (if that's an array object).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.