【发布时间】:2017-03-29 20:51:26
【问题描述】:
我正在使用现有的 Web Api (ASP.NET Core) 在 Angular 2 中实现一个新的 Web 应用程序,但是我在使用 HTTP Post 时遇到了问题。在搜索了各种信息后,我仍然无法解决这个问题,我的 Web API 仍然从 angular post 接收空参数。
我需要看看这里出了什么问题。 这是我的 Angular 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,
}
网络 API:
[HttpPost]
[Route("Register")]
public void Register([FromBody] UserRegisterViewModel userVm)
{
}
ViewModel WEB API:
public class UserRegisterViewModel
{
public string name { get; set; }
public string password { get; set; }
public string email { get; set; }
}
谁能告诉我哪里错了?
【问题讨论】:
-
调试代码的时间:使用开发工具捕获从浏览器发送到服务器的内容并检查,您应该能够检查整个 http 请求。如果这没有帮助,请尝试使用邮递员或提琴手让它在您的客户端代码之外执行。
标签: javascript asp.net angular asp.net-web-api http-post