Javascript Angular 2 http post空Web Api核心
我正在angular 2中使用现有的web Api(ASP.NET内核)实现一个新的web应用程序,但是我在使用HTTP Post时遇到了问题。在搜索了所有类型的信息之后,我仍然无法解决这个问题,我的Web API仍然从angular post接收空参数 我要看看这里出了什么问题。 这是我的Angular 2代码:Javascript Angular 2 http post空Web Api核心,javascript,asp.net,angular,asp.net-web-api,http-post,Javascript,Asp.net,Angular,Asp.net Web Api,Http Post,我正在angular 2中使用现有的web Api(ASP.NET内核)实现一个新的web应用程序,但是我在使用HTTP Post时遇到了问题。在搜索了所有类型的信息之后,我仍然无法解决这个问题,我的Web API仍然从angular post接收空参数 我要看看这里出了什么问题。 这是我的Angular 2代码: posted(event) { @Injectable() export class httpTestComponent { constructor(public http
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)
{
}
ViewModel 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)
Angular将尝试将已经序列化的JSON字符串序列化为对象,如果您不这样做,这就是您遇到问题的原因
源代码:没有理由添加
JSON.stringify()
。尝试删除它
编辑:
删除JSON.stringify()
或删除正文数据周围的大括号:
this.http.post('http://localhost:10832/api/Account/Register', pedido, options)
Angular将尝试将已经序列化的JSON字符串序列化为对象,如果您不这样做,这就是您遇到问题的原因
源代码:angular2文档和示例中另有说明。您可以发布一些文档来支持您的声明吗?您在
中写的所有内容都是正确的,请删除正文周围的大括号….
和on。这一点之前的一切都不准确,如果你删除它,你的答案应该可以解决老年退休金问题。那是我的问题!非常感谢,伙计,我取下了牙套,一切正常@Igor,如果您查看源代码,就会发现如果请求对象的主体是object类型,那么它将被JSON.stringify()序列化。自己动手做是不必要的。谢谢。:)angular2文档和示例中另有说明。您可以发布一些文档来支持您的声明吗?您在中写的所有内容都是正确的,请删除正文周围的大括号….
和on。这一点之前的一切都不准确,如果你删除它,你的答案应该可以解决老年退休金问题。那是我的问题!非常感谢,伙计,我取下了牙套,一切正常@Igor,如果您查看源代码,就会发现如果请求对象的主体是object类型,那么它将被JSON.stringify()序列化。自己动手做是不必要的。谢谢。:)调试代码的时间到了:使用开发人员工具捕获从浏览器发送到服务器的内容,并检查是否可以检查整个http请求。如果没有帮助,那么尝试使用postman或fiddler使其在客户端代码之外执行。调试代码的时间:使用开发人员工具捕获从浏览器发送到服务器的内容并进行检查,您应该能够检查整个http请求。如果这没有帮助,那么尝试使用postman或fiddler使其在客户端代码之外执行。