无法使用Wcf服务在Angular JS中基于Id返回单个记录

时间:2017-10-31 01:14:30

标签: javascript angularjs wcf

我试图通过使用wcf Rest服务在Angular JS应用程序中检索单个记录。我在谷歌浏览器的控制台窗口中检查。我得到了以下错误..

     [OperationContract]
            [WebInvoke(Method = "GET",
              RequestFormat = WebMessageFormat.Json,
              ResponseFormat = WebMessageFormat.Json,
              UriTemplate = "/AccountBalanceCheek")]
            AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber);

这是interfce ..

   public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)
        {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();

                var cmd = new SqlCommand("SELECT * FROM Current_Account_Details WHERE Account_Number = '" + accountNumber.Account_Number + "'", conn);


                cmd.CommandType = CommandType.Text;

                var reader = cmd.ExecuteReader();
                //read the result of the execute command.
                while (reader.Read())
                {
                    //assuming that your property is the same as your table schema. refer to your table schema Current_Account_Details

                    accountNumber.Account_Number= reader["Account_Number"].ToString();
                    accountNumber.Account_Creation_Date = reader["Account_Creation_Date"].ToString();

                    accountNumber.Account_Type = reader["Account_Type"].ToString();
                    accountNumber.Branch_Sort_Code = reader["Branch_Sort_Code"].ToString();
                    accountNumber.Account_Fees = reader["Account_Fees"].ToString();
                    accountNumber.Account_Balance = reader["Account_Balance"].ToString();
                    accountNumber.Over_Draft_Limit = reader["Over_Draft_Limit"].ToString();
                }
                return accountNumber;
            }
        }

这是实施。

var app = angular.module("WebClientModule", [])
    .controller('Web_Client_Controller', ["$scope", 'myService', function ($scope, myService) {
    var Id = $scope.Account_Number;
    $scope.search = function (Id) {
        var promiseGetSingle = myService.getbyId(Id);

        promiseGetSingle.then(function (pl) {
            var res = pl.data;
            $scope.Account_Number = res.Account_Number;
            $scope.Account_Creation_Date = res.Account_Creation_Date;
            $scope.Account_Type = res.Account_Type;
            $scope.Branch_Sort_Code = res.Branch_Sort_Code;
            $scope.Account_Fees = res.Account_Fees;
            $scope.Account_Balance = res.Account_Balance;
            $scope.Over_Draft_Limit = res.Over_Draft_Limit;

            //   $scope.IsNewRecord = 0;
        },
            function (errorPl) {
                console.log('failure loading Employee', errorPl);
            });
    }
    }]);




app.service("myService", function ($http) {

    this.getbyId = function (Id) {
        return $http.get("http://localhost:52098/HalifaxIISService.svc/AccountBalanceCheek" + Id);
    };
   })

这是我的脚本代码..

@{
    Layout = null;
}

<!DOCTYPE html>

<html ng-app="WebClientModule">
<head>
    <meta name="viewport" content="width=device-width" />

    <title>AccountBalance</title>
    <script src="~/Scripts/angular.min.js"></script>
    <script src="~/RegistrationScript/AccountBalance.js"></script>

</head>
<body>
    <div ng-controller="Web_Client_Controller">
        Enter Account_Number: <input type="text" ng-model="Account_Number" />
        <input type="button"  value="search" ng-click="search(Account_Number)" />

            <table id="tblContainer" data-ng-controller="Web_Client_Controller">
                <tr>
                    <td>
                        <table style="border: solid 2px Green; padding: 5px;">
                            <tr style="height: 30px; background-color: skyblue; color: maroon;">
                                <th></th>
                                <th>Account Number</th>
                                <th>Account Creation Date</th>
                                <th>Account Type</th>
                                <th>Branch Sort Code</th>
                                <th>Account Fees</th>
                                <th>Account Balance</th>
                                <th>Over Draft Limit</th>

                                <th></th>
                                <th></th>
                            </tr>
                            <tbody data-ng-repeat="user in Users">
                                <tr>
                                    <td></td>
                                    <td><span>{{user.Account_Number}}</span></td>
                                    <td><span>{{user.Account_Creation_Date}}</span></td>
                                    <td><span>{{user.Account_Type}}</span></td>
                                    <td><span>{{user.Branch_Sort_Code}}</span></td>

                                    <td><span>{{user.Account_Fees}}</span></td>
                                    <td><span>{{user.Account_Balance}}</span></td>
                                    <td><span>{{user.Over_Draft_Limit}}</span></td>
                                    <td>

                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td>

                        <div style="color: red;">{{Message}}</div>

                    </td>
                </tr>
            </table>
        </div>
        </body>


</html>

这是html。

try {
} catch (const std::runtime_error&) {
}

她是Google Chrome中的应用结果..

click here to see the oput put

1 个答案:

答案 0 :(得分:1)

如果您收到404 not fount错误,则表示您的端点地址出现问题。首先尝试使用fiddler或post man来使用您的服务,并确保您的服务成功加载。然后跟踪您的角度请求以确保您的客户端代码正常工作。