在 Angular Post 请求上收到 400 个错误请求,我该如何解决这个问题



请我在 angular 和 Django 项目上需要帮助,当我发出 post 请求时我收到 400 错误响应错误,项目工作流程如下,用户注册(成功(然后被重定向到个人资料表单然后当我在填写个人资料后尝试发出 POST 请求时,我得到 400 错误, 下面是我的代码和 d err 响应

角度服务.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));
}
}

姜戈网址

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

姜戈视图

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

错误响应

错误请求:/用户/配置文件 [14/5月/2020 17:07:04]"POST/user/profiles HTTP/1.1" 400 46

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

在文档中,它指的是"如果为创建对象提供的请求数据无效,则将返回 400 Bad Request 响应,错误详细信息作为响应的正文。 因此,您发送的配置文件无效。要查看哪个部分出错,请记录正文,或者您可以覆盖和调试CreateApiView的post方法

对我来说值得一提的是,我收到了 400 错误,因为我的 DJANGO 设置中允许的主机不正确。它让我措手不及,因为我以为我会得到 403 而不是 400 这样的东西。

ALLOWED_HOSTS = ['yourhost.com']

也不要像我一样做甜甜圈,在你的主机字符串中包含HTTP://

最新更新