如何发布到firebase数据库json

时间:2017-01-09 03:05:57

标签: angular firebase angularfire2

我正在尝试使用angular2中的api.service发布到firebase数据库。 我想以json的身份发帖。这是我的listing.service:

listPlace(place: Place) {
    return this.apiService.post(this.path, place)
    .do(savedPlace => this.storeHelper.add('places', savedNote))
  }

这是我的api.service:

export class ApiService {
  headers: Headers = new Headers({
    'Content-Type': 'application/json',
    Accept: 'application/json'
  });

  api_url: string = 'https://someapp-94b34.firebaseio.com/';
  constructor(private http: Http, private af: AngularFire) {}
private getJson(response: Response) {
    return response.json();
  }

  private checkForError(response: Response): Response {
    if (response.status >= 200 && response.status < 300) {
      return response;
    } else {
      var error = new Error(response.statusText)
      error['response'] = response;
      console.error(error);
      throw error;
    }
  }
post(path: string, body): Observable<any> {
    return this.http.post(
      `${this.api_url}${path}`,
      JSON.stringify(body),
      { headers: this.headers }
    )
    .map(this.checkForError)
    .catch(err => Observable.throw(err))
    .map(this.getJson)
  }

当我这样做并尝试发布时,我不允许这种错误方法。我该怎样做才能让它发挥作用?

zone.js:1645 OPTIONS https://someapp-94b34.firebaseio.com/places 405 (Method Not Allowed)
scheduleTask @ zone.js:1645
ZoneDelegate.scheduleTask @ zone.js:255
Zone.scheduleMacroTask @ zone.js:168
(anonymous) @ zone.js:1669
send @ VM4525:3
(anonymous) @ xhr_backend.js:125
Observable.subscribe @ Observable.js:45
MapOperator.call @ map.js:54
Observable.subscribe @ Observable.js:42
CatchOperator.call @ catch.js:31
Observable.subscribe @ Observable.js:42
MapOperator.call @ map.js:54
Observable.subscribe @ Observable.js:42
DoOperator.call @ do.js:63
Observable.subscribe @ Observable.js:42
ListComponent.onCreateNote @ list.component.ts:29
View_ListComponent0.handleEvent_5 @ component.ngfactory.js:176
(anonymous) @ view.js:664
schedulerFn @ async.js:103
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
ListcreatorComponent.onCreateNote @ listcreator.component.ts:23
View_ListcreatorComponent0.handleEvent_2 @ component.ngfactory.js:277
(anonymous) @ view.js:664
schedulerFn @ async.js:103
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:77
NgForm.onSubmit @ ng_form.js:192
Wrapper_NgForm.handleEvent @ wrapper.ngfactory.js:31
View_ListcreatorComponent0.handleEvent_2 @ component.ngfactory.js:275
(anonymous) @ view.js:664
(anonymous) @ dom_renderer.js:490
ZoneDelegate.invokeTask @ zone.js:275
onInvokeTask @ ng_zone.js:260
ZoneDelegate.invokeTask @ zone.js:274
Zone.runTask @ zone.js:151
ZoneTask.invoke @ zone.js:345
list:1 XMLHttpRequest cannot load https://someapp-94b34.firebaseio.com/places. Response for preflight has invalid HTTP status code 405

0 个答案:

没有答案