首页 > 解决方案 > 401 Unauthorized with Ionic 5 + Angular 9 with POST request

问题描述

我正在以下函数中发出发布请求:

addPlace(title: string,description: string, price: number) {
const newPlace = new Place(Math.random().toString(), title, description, 'https://i.insider.com/5d0150e06fc9200b5a05f042?width=1136&format=jpeg',price, this.authService.userId );
return this.http.post('https://hotelapp-2011f.firebaseio.com/offered-places.json', { ...newPlace, id:null}).subscribe(response => {
  console.log(response);

});

但我收到 401 Unauthorized 错误:

HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "Unauthorized", "https://hotelapp-2011f.firebaseio.com/offered-places.json", ok: false, …}
error: {error: "Permission denied"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for https://hotelapp-2011f.firebaseio.com/offered-places.json: 401 Unauthorized"
name: "HttpErrorResponse"
ok: false
status: 401
statusText: "Unauthorized"
url: "https://hotelapp-2011f.firebaseio.com/offered-places.json"
__proto__: HttpResponseBase

请帮我解决这个问题。

标签: angularionic-frameworkhttp-post

解决方案


这是因为您的服务器拒绝了您的请求。可能是因为身份验证。检查您发送的标题。您是否正确发送了身份验证令牌?检查它们,然后重试。


推荐阅读