admin管理员组文章数量:1406949
I am implementing a new web application in angular 2, using an existing Web Api (ASP.NET Core), but i am having trouble with HTTP Post. After searching all kind of information, I still not abble to resolve this problem and my Web API still receiving null params from angular post.
I need to see what is wrong here. Here is my Angular 2 code:
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();
};
ViewModel:
export interface UserViewModel {
name: string,
password: string,
email: string,
}
Web 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; }
}
Can someone tell me where Am I wrong?
I am implementing a new web application in angular 2, using an existing Web Api (ASP.NET Core), but i am having trouble with HTTP Post. After searching all kind of information, I still not abble to resolve this problem and my Web API still receiving null params from angular post.
I need to see what is wrong here. Here is my Angular 2 code:
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();
};
ViewModel:
export interface UserViewModel {
name: string,
password: string,
email: string,
}
Web 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; }
}
Can someone tell me where Am I wrong?
Share Improve this question asked Nov 15, 2016 at 20:14 André AzevedoAndré Azevedo 2371 gold badge4 silver badges13 bronze badges 1- Time to debug your code: Capture what is sent to the server from the browser using the developer tools and check that, you should be able to check the entire http request. If that does not help then try to use postman or fiddler to get it to execute outside of your client code. – Igor Commented Nov 15, 2016 at 20:32
1 Answer
Reset to default 7There is no reason to add JSON.stringify()
. Try removing it.
EDIT:
Either remove JSON.stringify()
or remove the braces around body data:
this.http.post('http://localhost:10832/api/Account/Register', pedido, options)
Angular will try to serialize already serialized JSON string as an object, if you leave it this way and that's why you're having an issue.
Source code: Link
本文标签: javascriptAngular 2 http post null Web Api CoreStack Overflow
版权声明:本文标题:javascript - Angular 2 http post null Web Api Core - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744957410a2634440.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论