在angular post request上得到400个坏的请求,如何解决这个问题?

问题描述 投票:0回答:1

我需要在我的angular和django项目上得到帮助,当我发出一个post请求时,我得到一个400坏的响应错误,项目的工作流程如下,用户注册(成功),然后被重定向到一个profile form,然后当我尝试在填写profile后发出post请求时,我得到一个400err,以下是我的代码和err响应。

angular services.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)
            );
    }

创建配置文件组件.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 urls

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

django views

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

误答

坏的请求:userprofiles[14May2020 17:07:04]"POST userprofiles HTTP1.1" 400 46。

如果有人能帮助我,我将很高兴

django angular django-rest-framework
1个回答
0
投票

文件,它指的是 "如果为创建对象提供的请求数据无效,将返回一个400 Bad Request响应,并将错误细节作为响应的主体。"所以,你发送的配置文件是无效的。要想知道是哪个部分出了问题,请记录正文,或者你可以覆盖和调试post方法的 创建ApiView

© www.soinside.com 2019 - 2024. All rights reserved.