Wcf REst服务无法接受Angular JS Application的输入值

时间:2017-10-12 02:36:20

标签: javascript c# angularjs wcf

我正在将Wcf服务用于Angular JS应用程序,但它根据我的预期不能正常工作。我正在尝试使用Sql事务方法将钱汇入一个帐户到另一个帐户。当我点击角度js应用程序中的提交按钮时,它无法将输入值发布到wcf服务。我检查控制台窗口进入谷歌浏览器我点击提交按钮时确实发现任何错误

这是名为Money Transfer ...的本地类......

public static void main( String[] args){

  String textToDoStuffTo = "Change this text";  

  //you can do it like this
  textToDoStuffTo = normalizeText(textToDoStuffTo);
  textToDoStuffTo  = obify(textToDoStuffTo);

  //or you can combine them like this
  normalizeText(obify(textToDoStuffTo));

}

static String normalizeText(String text){
    //changes the text
    return text;
}

static String obify(String text){
    //changes the text
    return text;
}

这是我的界面。

{
    [DataContract]
    public class MoneyTransfer
    {
        string sender_account_no;
        string sender_name;
        string sender_sort_code;
        string amount1;
        string transcation_type;
        string date;
        string receiver_account_no;
        string receiver_name;
        string receiver_sort_code;
        string amount2;
        string transcation_type1;
        string date1;

        [DataMember]
        public string Sender_Account_No
        {
            get { return sender_account_no; }
            set { sender_account_no = value; }


        }
        [DataMember]
        public string Sender_Name
        {
            get { return sender_name; }
            set { sender_name = value; }

        }
        [DataMember]
        public string Sender_Sort_Code
        {
            get { return sender_sort_code; }
            set { sender_sort_code = value; }
        }
        [DataMember]
        public string Amount
        {
            get { return amount1; }
            set { amount1 = value; }
        }
        [DataMember]
        public string Transcation_Type
        {
            get { return transcation_type; }
            set { transcation_type = value; }

        }
        [DataMember]
        public string Date
        {
            get { return date; }
            set { date = value; }


        }
        [DataMember]
        public string Receiver_Account_No
        {
            get { return receiver_account_no; }
            set { receiver_account_no = value; }


        }
        [DataMember]
        public string Receiver_Name
        {
            get { return receiver_name; }
            set { receiver_name = value; }

        }
        [DataMember]
        public string Receiver_Sort_Code
        {
            get { return receiver_sort_code; }
            set { receiver_sort_code = value; }
        }
        [DataMember]
        public string Amount1
        {
            get { return amount2; }
            set { amount2 = value; }
        }
        [DataMember]
        public string Transcation_Type1
        {
            get { return transcation_type1; }
            set { transcation_type1 = value; }

        }
        [DataMember]
        public string Date1
        {
            get { return date1; }
            set { date1 = value; }


        }

    }
}

这是我的实施。

 [OperationContract]
        [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/MoneyTranfer")]
       bool  MoneyTranfer(MoneyTransfer mopneyTransfer);

这是脚本代码。

  public bool MoneyTranfer(MoneyTransfer mopneyTransfer)
            {

                int amount = System.Convert.ToInt32(mopneyTransfer.Amount);
                int amount1 = System.Convert.ToInt32(mopneyTransfer.Amount1);

                SqlConnection cn = new SqlConnection(ConnectionString);
                string sql = "select Account_Balance from Current_Account_Details where Account_Number='" + mopneyTransfer.Sender_Account_No + "'";

                SqlCommand cmd = new SqlCommand(sql, cn);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();

                //amount = int.Parse(cmd.ExecuteScalar().ToString());

                if (amount > 0)
                {
                    int b;
                    int b1;
                    SqlCommand cmd1 = new SqlCommand();
                    SqlTransaction trans;
                    if (cn.State == ConnectionState.Closed)
                        cn.Open();
                    trans = cn.BeginTransaction();

                    cmd1.Connection = cn;
                    cmd1.CommandType = CommandType.Text;
                    cmd1.Transaction = trans;
                    cmd1.CommandText = "update Current_Account_Details set Account_Balance=Account_Balance-'" + mopneyTransfer.Amount + "' where Account_Number='" + mopneyTransfer.Sender_Account_No + "'";
                    b = cmd1.ExecuteNonQuery();

                    cmd1.CommandText = "update Reward_Account_Details set Account_Balance=Account_Balance+'" + mopneyTransfer.Amount1 + "' where Account_Number='" + mopneyTransfer.Receiver_Account_No + "'";
                    b1 = cmd1.ExecuteNonQuery();
                    if (b == 1 && b1 == 1)
                    {
                        trans.Commit();
                        using (SqlConnection con = new SqlConnection(ConnectionString))
                        {
                            //Create the SqlCommand object
                            //Create the SqlCommand object
                            SqlCommand cmd3 = new SqlCommand("Current_Account_WITHDRAR", con);
                            //Specify that the SqlCommand is a stored procedure
                            cmd3.CommandType = System.Data.CommandType.StoredProcedure;

                            //Add the input parameters to the command object
                            cmd3.Parameters.AddWithValue("@Account_Number", mopneyTransfer.Sender_Account_No);
                            cmd3.Parameters.AddWithValue("@Account_Holder_Name", mopneyTransfer.Sender_Name);
                            cmd3.Parameters.AddWithValue("@Amount", mopneyTransfer.Amount);


                            cmd3.Parameters.AddWithValue("@Sort_Code", mopneyTransfer.Sender_Sort_Code);
                            cmd3.Parameters.AddWithValue("@Transcation_Type", mopneyTransfer.Transcation_Type);
                            cmd3.Parameters.AddWithValue("@Date", mopneyTransfer.Date);

                            SqlCommand cmd2 = new SqlCommand("Reward_Account_Dposit", con);
                            //Specify that the SqlCommand is a stored procedure
                            cmd2.CommandType = System.Data.CommandType.StoredProcedure;

                            //Add the input parameters to the command object
                            cmd2.Parameters.AddWithValue("@Account_Number", mopneyTransfer.Receiver_Account_No);
                            cmd2.Parameters.AddWithValue("@Account_Holder_Name", mopneyTransfer.Receiver_Name);
                            cmd2.Parameters.AddWithValue("@Amount", mopneyTransfer.Amount1);


                            cmd2.Parameters.AddWithValue("@Sort_Code", mopneyTransfer.Receiver_Sort_Code);
                            cmd2.Parameters.AddWithValue("@Transcation_Type", mopneyTransfer.Transcation_Type1);
                            cmd2.Parameters.AddWithValue("@Date", mopneyTransfer.Date1);




                            //Open the connection and execute the query

                            con.Open();
                            cmd2.ExecuteNonQuery();

                            cmd3.ExecuteNonQuery();
                            return true;
                            //con.Close();
                        }

                    }
                    else
                        trans.Rollback();
                    return false;
                }
                return false;

            }

这是HTML

/// <reference path="../angular.min.js" />  



var app = angular.module("WebClientModule", [])

    .controller('Web_Client_Controller', ["$scope", 'myService', function ($scope, myService) {

        $scope.OperType = 1;

        //1 Mean New Entry  

        //To Clear all input controls.  
        function ClearModels() {
            $scope.OperType = 1;

            $scope.Sender_Account_No = "";
            $scope.Sender_Name = "";
            $scope.Sender_Sort_Code = "";
            $scope.Amount = "";
            $scope.Transcation_Type = "";
            $scope.Date = "";

            $scope.Receiver_Account_No = "";
            $scope.Receiver_Name = "";
            $scope.Receiver_Sort_Code = "";
            $scope.Amount1 = "";
            $scope.Transcation_Type1 = "";
            $scope.Date1 = "";

        }
        $scope.transfer = function () {
            var User = {
                Sender_Account_No: $scope.Sender_Account_No,
                Serder_Name: $scope.Serder_Name,
                Sender_Sort_Code: $scope.Sender_Sort_Code,
                Amount: $scope.Amount,
                Transcation_Type: $scope.Transcation_Type,
                Date:$scope.Date ,

                Receiver_Account_No: $scope.Receiver_Account_No ,
                Receiver_Name: $scope.Receiver_Name,
                Receiver_Sort_Code: $scope.Receiver_Sort_Code ,
                Amount1: $scope.Amount1,
                Transcation_Type1: $scope.Transcation_Type1,
                Date1:$scope.Date1 

            };
            if ($scope.OperType === 1) {
                var promisePost = myService.post(User);
                promisePost.then(function (pl) {
                    $scope.Id = pl.data.Id;
                    $scope.msg = "Operation is successful";


                }, function (err) {
                    $scope.msg = "Operation is failed !";
                    console.log("Some error Occured" + err);
                });
            } else {

            }

        };



    }]);

app.service("myService", function ($http) {
    //Create new record  
    this.post = function (User) {
        var request = $http({
            method: "post",
            url: "http://localhost:52098/HalifaxIISService.svc/MoneyTranfer",
            data: JSON.stringify(User)
        });
        return request;

    }

})

以下是调试模式的屏幕截图..

.NET Native General Troubleshooting

这是我运行应用程序时的屏幕截图..

Click here to see the result

任何反馈或建议都是适当的。感谢

网络标签.. click here to see the out put

1 个答案:

答案 0 :(得分:1)

尝试更改

<input type="text" id="amount_to_recive" required data-ng-model="Amount_to_Recive" require="" />

<input type="text" id="amount_to_recive" required data-ng-model="Amount1" require="" />

等...... data-ng-model属性是映射到$scope

的重要属性