嗨,我是 Angular 新手, 现在我从一个示例登录页面开始,该页面传递包含用户名和密码的 userEntity。 userEntity 是一个对象/实体,是 webapi。
问题: 当我对登录按钮进行 http Post 调用时,单击我发现调用 webapi2 服务的 Angular ,其中 userEntity 不为 null,但对象的参数(即用户名和密码)变为 null。
当我尝试从 Postman/fiddler 发布数据时,Web Api2 可以正常工作。 不确定从 Angular 调用时出现什么问题。 任何帮助将非常感激。提前致谢。
WEB API2 代码如下:
[AllowAnonymous]
[HttpPost]
[Route("AuthenticateUser", Name = "AuthenticateUser")]
public HttpResponseMessage AuthenticateUser([FromBody]userEntity userentity)
{
string username = userentity.username;
string password = userentity.password;
return Request.CreateResponse(HttpStatusCode.OK, userLibraryService.AuthenticateUser(username, password));
}
下面的 Angular 代码:
factory.login = function (email, password) {
return $http.post(serviceBase + 'AuthenticateUser', { userentity: { username: ’<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a7e6f797e7f796f784a7e6f797e24696567" rel="noreferrer noopener nofollow">[email protected]</a>', password : 'pass1335' } }).then(
function (results) {
var loggedIn = results.data.status;;
return loggedIn;
});
};
输出: 我看到 userEntity 不为空,但一旦请求到达 webapi2 服务,用户名和密码就为空
最佳答案
尝试发送仅包含用户名和密码属性的对象:
return $http.post(serviceBase + 'AuthenticateUser', { username: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f18594828584829483b185948285df929e9c" rel="noreferrer noopener nofollow">[email protected]</a>', password: 'pass1335' }).then(
function (results) {
var loggedIn = results.data.status;;
return loggedIn;
});
};
您的示例也有 '
而不是 '
,不确定是否是拼写错误。
关于c# - Angular 到 webapi httppost 调用发送用户对象,一旦它们到达用户 obj 下的 webapi 服务属性将变为 null(usrnm/pswd),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32994496/