首页 > 解决方案 > Angular 6 将数据发布到休息 API 但返回错误 500

问题描述

我需要将数据发布到其余 API,但它返回错误 500。当我在 Postman 中测试 API 时,API 正在工作。

邮递员回应

身份验证.service.ts

register(email: string, fullName: string,contactNo: string) {
    var data = {
        'email' : email,
        'fullName' : fullName,
        'contactNo': contactNo
    };

    return this.http.post<any>(`http://localhost:80/user`, {data})
        .pipe(map(user => {

            JSON.stringify(user);
            return user;
        }));
}

标签: angular

解决方案


您正在发布一个具有名为 data 的属性的对象,该属性设置为您的对象数据。从数据周围删除花括号。你的身体看起来像

{
  data: {
    email: email,
    fullName: fullName,
    contactNo: contactNo
  }
}

尝试

register(email: string, fullName: string,contactNo: string) {
    var data = {
        'email' : email,
        'fullName' : fullName,
        'contactNo': contactNo
    };

    return this.http.post<any>(`http://localhost:80/user`, data)
        .pipe(map(user => {

            JSON.stringify(user);
            return user;
        }));
}

推荐阅读