angular - 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
请帮我解决这个问题。
解决方案
这是因为您的服务器拒绝了您的请求。可能是因为身份验证。检查您发送的标题。您是否正确发送了身份验证令牌?检查它们,然后重试。
推荐阅读
- applescript-objc - 使用 AppleScript-Objc 确定进度指示器
- java - 如何在@PostMapping 中返回值
- java - 对目标文件的引用。为什么/如何创建和使用它?
- python - 从数据库读取时在 Dask 数据帧中设置分区大小的问题
- ruby-on-rails - 如何在应用程序中制作自定义配置文件(它包括站点名称、徽标和 css 等变量),以便为不同的网站重用相同的代码库?
- android - 在 onMapReady 内部,Picasso 不会第一次加载图像,而是在重新创建活动后加载图像
- python - 在同步代码线程内具有回调的 Django 通道
- javascript - 如何通过套接字连接发送用户电子邮件地址?
- rust - 将值传递给参数之外的函数
- c# - 当事件处理程序代码在后台线程中执行?