Javascript Wcf服务使用Angular Js应用程序引发空引用异常
我正在将Wcf服务消费到Angular JS应用程序中。我使用类库项目创建了wcf服务。我将其托管到Service.svc文件中。当我点击提交按钮时,我在文本框中输入了所需信息,它显示了以下错误Javascript Wcf服务使用Angular Js应用程序引发空引用异常,javascript,c#,angularjs,wcf,ado.net,Javascript,C#,Angularjs,Wcf,Ado.net,我正在将Wcf服务消费到Angular JS应用程序中。我使用类库项目创建了wcf服务。我将其托管到Service.svc文件中。当我点击提交按钮时,我在文本框中输入了所需信息,它显示了以下错误 System.NullReferanceException: Object reference not set an instance of an object. Angular JS application showing following error .. POST http://localho
System.NullReferanceException: Object reference not set an instance of an object.
Angular JS application showing following error ..
POST http://localhost:52098/HalifaxIISService.svc/RegisterUser 400 (Bad Request)
这里是接口
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "/RegisterUser")]
bool RegisterUser(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;
}
}
}
下面是Script.Js文件代码
/// <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 = "";
$scopr.Email = "";
}
$scope.createuser = function () {
var User = {
Username: $scope.Username,
Password: $scope.Password,
Email: $scope.Email
};
if ($scope.OperType === 1) {
var promisePost = myService.post(User);
promisePost.then(function (pl) {
$scope.User_Id = pl.data.User_Id;
window.location.href = "/Login/Index";
ClearModels();
}, function (err) {
$scope.msg = "Password Incorrect !";
console.log("Some error Occured" + err);
});
}
}
}]);
app.service("myService", function ($http) {
//Create new record
this.post = function (User) {
var request = $http({
method: "post",
url: "http://localhost:52098/HalifaxIISService.svc/RegisterUser",
data: User
});
return request;
}
})
//
var app=angular.module(“WebClient模块”,[])
.controller('Web_客户端_控制器',[“$scope”,'myService',函数($scope,myService){
$scope.OperType=1;
//1是指新条目
//清除所有输入控件。
函数ClearModels(){
$scope.OperType=1;
$scope.Username=“”;
$scope.Password=“”;
$scopr.Email=“”;
}
$scope.createuser=函数(){
变量用户={
用户名:$scope.Username,
密码:$scope.Password,
电子邮件:$scope.Email
};
如果($scope.OperType==1){
var promisePost=myService.post(用户);
promisePost.then(功能(pl){
$scope.User\u Id=pl.data.User\u Id;
window.location.href=“/Login/Index”;
ClearModels();
},函数(err){
$scope.msg=“密码不正确!”;
console.log(“发生了一些错误”+err);
});
}
}
}]);
app.service(“myService”,函数($http){
//创造新记录
this.post=函数(用户){
var请求=$http({
方法:“张贴”,
url:“http://localhost:52098/HalifaxIISService.svc/RegisterUser",
数据:用户
});
返回请求;
}
})
下面是调试模式的屏幕截图。。
这是单击提交按钮的屏幕截图。
我不知道我为什么会犯这样的错误。任何帮助都将不胜感激 null引用表示UserLogin对象为null。我首先检查以下内容 确保UserLogin是一个数据协定,其数据成员与用户名、密码和电子邮件值相对应。在post请求中,设置用户对象的字符串
data : JSON.stringify(User)
查看下面的链接了解更多信息,我会确保$http设置了正确的内容类型
编辑:我建议使用不同于类名的值命名变量。我不确定c如何处理这个问题。空引用意味着UserLogin对象为空。我首先检查以下内容 确保UserLogin是一个数据协定,其数据成员与用户名、密码和电子邮件值相对应。在post请求中,设置用户对象的字符串
data : JSON.stringify(User)
查看下面的链接了解更多信息,我会确保$http设置了正确的内容类型
编辑:我建议使用不同于类名的值命名变量。我不确定c如何处理这个问题。正如您所看到的,我提供了所有代码。UserLogin类获得了所有具有数据契约和数据成员属性的属性。我不知道该怎么说,正如您在问题中看到的,我提供了所有代码。UserLogin类获得了所有具有数据协定和数据成员属性的属性。我不知道该怎么说