Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 使用Angular和Firebase保存数据_Json_Typescript_Firebase_Firebase Realtime Database - Fatal编程技术网

Json 使用Angular和Firebase保存数据

Json 使用Angular和Firebase保存数据,json,typescript,firebase,firebase-realtime-database,Json,Typescript,Firebase,Firebase Realtime Database,我正在创建一个Deckbuilder,我有一副卡片和一个data-storage.service来存储和获取Firebase中的卡片。 我有一个组件牌组详细信息,显示所选牌组的详细信息,并允许移除或向牌组添加牌。 我正在尝试将修改后的卡存储在firebase中选定的卡组中 这是数据存储服务 @Injectable({ providedIn: "root" }) export class DataStorageService { constructor(private http: HttpCl

我正在创建一个Deckbuilder,我有一副卡片和一个data-storage.service来存储和获取Firebase中的卡片。 我有一个组件牌组详细信息,显示所选牌组的详细信息,并允许移除或向牌组添加牌。 我正在尝试将修改后的卡存储在firebase中选定的卡组中

这是数据存储服务

@Injectable({ providedIn: "root" })
export class DataStorageService {
  constructor(private http: HttpClient, private deckService: DeckService) {}

  storeDecks() {
    const decks = this.deckService.getDecks();
    this.http
      .put("https://ang-cards.firebaseio.com/decks.json", decks)
      .subscribe((response) => {
        console.log(response);
        console.log("stored");
      });
  }
  fetchDecks() {
    return this.http
      .get<Deck[]>("https://ang-cards.firebaseio.com/decks.json")
      .subscribe((decks) => {
        decks
          ? this.deckService.setDecks(decks)
          : this.deckService.setDecks([]);
        console.log("fetching", decks);
      });
  }
  storeCards(i: number, cards: Card[]){
    this.http
    .put("https://ang-cards.firebaseio.com/decks/" + i + "/deckCards", cards)
    .subscribe((response) => {
      console.log(response);
      console.log("cards stored");
    });
  }
}

当我尝试存储卡时,会出现以下3个错误:

CORS策略已阻止从源“”访问“”处的XMLHttpRequest:对飞行前请求的响应未通过访问控制检查:它没有HTTP ok状态

PUT net::ERR\u失败

core.js:5882错误HttpErrorResponse{headers:HttpHeaders,状态:0,状态文本:“未知错误”,url:,ok:false,…}


要与Firebase实时数据库的REST API交互,URL必须以
.json
结尾。否则,您将尝试访问Firebase控制台,当您访问Firebase控制台时,它将返回一个跨原点错误

因此,代码应该如下所示:

this.http
    .put("https://ang-cards.firebaseio.com/decks/" + i + "/deckCards.json", cards)
    ...

要与Firebase实时数据库的REST API交互,URL必须以
.json
结尾。否则,您将尝试访问Firebase控制台,当您访问Firebase控制台时,它将返回一个跨原点错误

因此,代码应该如下所示:

this.http
    .put("https://ang-cards.firebaseio.com/decks/" + i + "/deckCards.json", cards)
    ...

您是否已搜索错误消息?CORS政策一经常会出现,因此可能会有一个很好的解释,说明您为什么会得到它,以及如何解决它。如果我没记错的话,那是因为您忘记在URL的末尾添加
.json
。您是否已经搜索了错误消息?CORS政策一经常会出现,因此可能会有一个很好的解释,说明您为什么会得到它,以及如何解决它。如果我没记错的话,那是因为你忘了在URL的末尾添加
.json