服务器使用wcf服务以角度Js响应状态为400(错误请求)

时间:2017-09-23 01:23:13

标签: javascript c# angularjs wcf

我在Angular Js应用程序中使用wcf服务。我试图通过调用Wcf服务使用Angular Js创建用户注册系统。当我在Angular Js应用程序中单击保存按钮时出现以下错误...

  

捕获了SyntaxError:意外的令牌<       :52098 / HalifaxIISService.svc / RegisterUser无法加载资源:服务器响应状态为400(错误请求)       Registration.js:38发生了一些错误[object object]

这是wcf服务方法。

[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
 UriTemplate = "/RegisterUser")]
bool RegisterUser(UserLogin UserLogin);


[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "/AuthenticateUser")]

bool AuthenticateUser(UserLogin UserLogin);

这是接口的实现..

public bool RegisterUser(UserLogin UserLogin)
{
    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    // SqlConnection is in System.Data.SqlClient namespace
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand cmd = new SqlCommand("spRegisterUser", con);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter username = new SqlParameter("@Username", UserLogin.Username);
        // FormsAuthentication calss is in System.Web.Security namespace
        string encryptedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(UserLogin.Password, "SHA1");
        SqlParameter password = new SqlParameter("@Password", encryptedPassword);
        SqlParameter email = new SqlParameter("@Email", UserLogin.Email);

        cmd.Parameters.Add(username);
        cmd.Parameters.Add(password);
        cmd.Parameters.Add(email);

        con.Open();
        int ReturnCode = (int)cmd.ExecuteScalar();
        if (ReturnCode == -1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
public bool AuthenticateUser(UserLogin UserLogin)
{
    // ConfigurationManager class is in System.Configuration namespace
    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    // SqlConnection is in System.Data.SqlClient namespace
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand cmd = new SqlCommand("spAuthenticateUser", con);
        cmd.CommandType = CommandType.StoredProcedure;

        //Formsauthentication is in system.web.security
        string encryptedpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(UserLogin.Password, "SHA1");

        //sqlparameter is in System.Data namespace
        SqlParameter paramUsername = new SqlParameter("@UserName", UserLogin.Username);
        SqlParameter paramPassword = new SqlParameter("@Password", encryptedpassword);

        cmd.Parameters.Add(paramUsername);
        cmd.Parameters.Add(paramPassword);

        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            int RetryAttempts = Convert.ToInt32(rdr["RetryAttempts"]);
            if (Convert.ToBoolean(rdr["AccountLocked"]))
            {
                return true;
            }
            else if (RetryAttempts > 0)
            {
                int AttemptsLeft = (4 - RetryAttempts);
                //lblMessage.Text = "Invalid user name and/or password. " +
                //    AttemptsLeft.ToString() + "attempt(s) left";
            }
            else if (Convert.ToBoolean(rdr["Authenticated"]))
            {
                return true;
            }

        }
        return false;
    }
} 

这是脚本文件。

/// <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.Username = "";
            $scope.Password = "";
            $scope.Email = "";

        }

        $scope.User = function () {
            var RegisterUser = {
                Username: $scope.Username,
                Password: $scope.Password,
                Email: $scope.Email

            };
            if ($scope.OperType === 1) {
                var promisePost = myService.post(RegisterUser);
                promisePost.then(function (pl) {
                    $scope.User_Id = pl.data.User_Id;
                    //window.location.href = "/Webogin/Welcome";

                    //ClearModels();
                }, function (err) {
                    $scope.msg = "Password Incorrect !";
                    console.log("Some error Occured" + err);
                });
            }
        }
    }]);

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

这是我点击保存按钮时的屏幕截图.. click here to see the result

0 个答案:

没有答案