【问题标题】:Angular JS Application Is Failed with Multi Post RequestAngular JS 应用程序因多次发布请求而失败
【发布时间】:2018-01-09 00:45:10
【问题描述】:

我正在使用来自 AngularJs 应用程序的 WCF 服务。我正在向该服务发布多个请求。通过这个请求,我正在检查用户信息。

这是引发错误的服务:

public bool cheekCreditScore(Credit_Crad credit)
{

    int i = 600;
    int j = 700;

    SqlConnection cn = new SqlConnection(ConnectionString);

    SqlCommand cmd = new SqlCommand("SELECT Credit_Score FROM Credit_Score WHERE Account_Number = '" + credit.account_number + "'", cn);
    cn.Open();
    cmd.Parameters.AddWithValue("Account_Number", credit.account_number);
    var value = cmd.ExecuteScalar();
    var da = new SqlDataAdapter(cmd);
    DataTable tbl = new DataTable();
    da.Fill(tbl);

    if (tbl.Rows.Count == 0)
    {
        //message = ("Account  is not exist Under this Name");
        return true;

    }
    else if ((Convert.ToDouble(i) < Convert.ToDouble(value)) && (Convert.ToDouble(value) <= Convert.ToDouble(j)))
    {

        // message = "Application Successful We can offer you " + Value1 + "Pound";
        return true;

    }

    else
    {
        // message = "Your application is unsuccessfull ";
        return false;

    }
    //return false;

}

这些是我遇到的错误:

这是 Angular JS Web 应用程序中的脚本代码,这是我正在向 Wcf Rest Service 发布多请求。

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.Tittle = "";
            $scope.First_Name = "";
            $scope.Last_Name = "";
            $scope.Gender = "";
            $scope.DOB = "";
            $scope.Mobile = "";
            $scope.House_No = "";

            $scope.Streent_Name = "";
            $scope.Country = "";
            $scope.Post_Code = "";
            $scope.Occupation = "";

            $scope.Account_Number = "";
        }
        $scope.CeditCardApplication = function () {
            var ApplicationDeatils = {
                Tittle: $scope.Tittle,
                First_Name: $scope.First_Name,
                Last_Name: $scope.Last_Name,
                Gender: $scope.Gender,
                DOB: $scope.DOB,
                Mobile: $scope.Mobile,
                House_No: $scope.House_No,
                Streent_Name: $scope.Streent_Name,
                Country: $scope.Country,
                Post_Code: $scope.Post_Code,
                Occupation: $scope.Occupation,
                Account_Number: $scope.Account_Number
            };
            myService.ApplicationDeatilsCheck(ApplicationDeatils).then(function (pl) {
                console.log(pl.data)
                if (pl.data) {

                    //$scope.Account_Number = pl.data.Account_Number;

                    $scope.msg = "User information is correct  !";                    

                        };

            }); 


            myService.ApplicationCreditScoreCheck(ApplicationDeatils).then(function (p2) {
                console.log(p2.data)

                if (p2.data) {

                    //$scope.Account_Number = p2.data.Account_Number;

                    $scope.msg = "We can offer you £6000";

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


        } // <-- missing }
    }]);



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

    this.ApplicationDeatilsCheck = function (ApplicationDeatils) {
        return $http.post("http://localhost:52098/HalifaxIISService.svc/CreateCurrentAccountCheck", JSON.stringify(ApplicationDeatils));
    }
    this.ApplicationCreditScoreCheck = function (ApplicationDeatils) {
        return $http.post("http://localhost:52098/HalifaxIISService.svc/cheekCreditScore", JSON.stringify(ApplicationDeatils));
    }

});

【问题讨论】:

  • 如果您有异常消息准确地告诉您问题出在服务上运行的 SQL 中,具体是什么让您认为您的客户端或它是第二个查询的事实有任何关系用它?这个问题中的大部分代码都是完全不相关的,应该删除。请阅读如何创建minimal reproducible example

标签: c# sql wcf


【解决方案1】:

使用参数的全部目的是避免字符串连接并防止 SQL 注入。您的代码既传递参数又连接:

SqlCommand cmd = new SqlCommand("SELECT Credit_Score FROM Credit_Score WHERE Account_Number = '" + credit.account_number + "'", cn);
cn.Open();
cmd.Parameters.AddWithValue("Account_Number", credit.account_number);

这显然是错误的,因为您传递了一个您的查询甚至不知道的参数。

只需将您的代码更改为:

SqlCommand cmd = new SqlCommand("SELECT Credit_Score FROM Credit_Score WHERE Account_Number = @Account_Number", cn);
cn.Open();
cmd.Parameters.AddWithValue("Account_Number", credit.account_number);

【讨论】:

  • @Mohammad 只有当credit.account_numbernull 时,你需要使用DbNull 来代替
  • 是的。它是空的。但我不知道是 ado.net 还是 angular js 的问题
  • @Mohammad 这可能是一个大小写问题(属性名称是 c# 中的 camelCase 和 Angular 中的 PascalCase,与正常情况相反)。您是否检查过 angular 实际上发送了该变量中的任何内容?用chrome查看请求数据
猜你喜欢
  • 2018-03-03
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多