Angular 2 http post null Web Api Core
问题描述:
我正在使用现有的Web Api(ASP.NET Core)在角度2中实现新的Web应用程序,但我在使用HTTP Post时遇到了问题。在搜索完所有类型的信息之后,我仍然无法解决这个问题,并且我的Web API仍然从角度帖子接收空参数。Angular 2 http post null Web Api Core
我需要看看这里有什么问题。 这里是我的角2码:
posted(event) {
@Injectable()
export class httpTestComponent {
constructor(public http: Http) {
};
posted(event) {
var user: UserViewModel = {
name: "angularUser",
password: "pw",
email: "angularMail"
}
let pedido = JSON.stringify(user);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post('http://localhost:10832/api/Account/Register', { pedido }, options)
.map(this.extractData).catch(this.handleError).subscribe();
};
视图模型:
export interface UserViewModel {
name: string,
password: string,
email: string,
}
的Web API:
[HttpPost]
[Route("Register")]
public void Register([FromBody] UserRegisterViewModel userVm)
{
}
视图模型WEB API:
public class UserRegisterViewModel
{
public string name { get; set; }
public string password { get; set; }
public string email { get; set; }
}
有人能告诉我我在哪里错了吗?
答
没有理由添加JSON.stringify()
。尝试删除它。
编辑:
要么删除JSON.stringify()
或删除周围体数据的括号:
this.http.post('http://localhost:10832/api/Account/Register', pedido, options)
角将尝试序列化已经序列化JSON字符串作为一个对象,如果你离开它这样,这就是为什么你有问题。
的源代码:Link
时间调试代码:捕捉什么是从浏览器中使用的开发工具发送到服务器并检查,你应该能够检查整个HTTP请求。如果这没有帮助,那就尝试使用邮递员或小提琴手来让它在客户端代码之外执行。 – Igor