[英]AngularJS $http.post to ASP.NET Web API controller with simple type argument
[英]Angularjs $http post doesnt binding with asp.net web api server model
我有下一个问题:Angularjs $ http post请求不要将服务器端的模型绑定到body属性。 已创建模型但具有null属性。 我已经使用Chrome Postman和firefox海报进行了测试。 所有的作品和装订。 所以问题应该是在一个问题上
local.signup = function(user) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
return $http.post(config.signupUrl, {displayName:user.displayName,email:user.email, password: user.password})
.then(function(response) {
if (config.loginOnSignup) {
shared.setToken(response);
} else if (config.signupRedirect) {
$location.path(config.signupRedirect);
}
return response;
});
};
和服务器端控制器方法
[HttpOptions]
[HttpPost]
[Route("signup")]
public IHttpActionResult Signup([FromBody] User model)
{
if (model == null || String.IsNullOrWhiteSpace(model.email))
return BadRequest("Email Address is required.");
if (String.IsNullOrWhiteSpace(model.displayName))
return BadRequest("Name is required.");
......
}
编辑用户模型
public class User
{
public string displayName { get; set; }
public string email { get; set; }
public string password { get; set; }
}
我找到了答案! 首先,可以使用jObject之类的
public IHttpActionResult Post([FromBody] jObject model){..}
但后来我消失了其他问题 - jObject模型带有反斜杠和角度POST请求转为OPTIONS请求。 原因是在CORS Deeping中,我在Thinktecture.dll中找到了允许CORS的解决方案。 有关配置的更多信息,您可以在下一篇文章中找到WebAPI,MVC和IIS中的CORS支持 。
在那之后,一切正常! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.