【问题标题】:getting 400 bad request on angular post request how can i fix this在角度发布请求上收到 400 个错误请求我该如何解决这个问题
【发布时间】:2020-05-16 22:54:30
【问题描述】:

请我在我的 Angular 和 django 项目上需要帮助,当我发出 400 错误响应错误时,项目工作流程如下,用户注册(成功)然后被重定向到一个配置文件表单,然后当我在填写配置文件后尝试发出发布请求时,我得到一个 400 错误,下面是我的代码和错误响应

角度服务.ts

    addProfile(profile: Iprofile): Observable<Iprofile>{
        const url = 'http://127.0.0.1:8000/user/profiles';
        return this.httpClient.post<Iprofile>(url, profile)
            .pipe(
                 map(response => response),
                 catchError(this.handleError)
            );
    }

创建配置文件component.ts

    addUpdateProfile(){
    if (this.profile) {
        this.dataService.updateProfile(this.profileForm.value)
        .subscribe(item => this.profiles.push(this.profileForm.value));
    }
    else {
        this.dataService.addProfile(this.profileForm.value)
        .subscribe(item => this.profiles.push(this.profileForm.value));
    }
  }

django 网址

path('profiles', CreateProfileView.as_view(), name='user-profile'),

django 视图

class CreateProfileView(generics.ListCreateAPIView):
queryset = Profile.objects.all()
serializer_class = UserDetailSerializer

错误响应

错误请求:/user/profiles [14/May/2020 17:07:04]“POST /user/profiles HTTP/1.1”400 46

如果有人可以帮助我,我会很高兴

【问题讨论】:

    标签: django angular django-rest-framework


    【解决方案1】:

    documentation中,它是指“如果为创建对象提供的请求数据无效,将返回400 Bad Request响应,错误详情作为响应正文。”因此,您发送的配置文件无效。要查看哪一部分出错了,请登录 body 或者您可以覆盖和调试 CreateApiView 的 post 方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      相关资源
      最近更新 更多