Angular 2 Http发布错误

时间:2016-03-14 23:49:08

标签: http angular rxjs observable ionic2

我有一个Ionic 2应用程序,可以从Facebook获得一些活动。我尝试使用batch containing multiple requests向图表Api发送帖子请求,但我的订阅始终返回此错误:

  

{" _body":"""状态":404,"状态文本":"确定" "头" {"客户通过&#34 ;,   [" shouldInterceptRequest"]}"类型":2" URL":" https://graph.facebook.com/"}

我的组件内的订阅:

this.eventListService.get()
  .subscribe(
    response => this.response = response,
    error => this.error = error
  )

EventListService:

import {Injectable} from '../../node_modules/angular2/core';
import {Http, Headers, URLSearchParams} from '../../node_modules/angular2/http';
import {Observable} from '../../node_modules/rxjs/Rx';
import {AuthenticationService} from '../user/authentication.service';

@Injectable()
export class EventListService {
  private fields: string = 'id, cover, name, start_time, place, attending_count, interested_count, rsvp_status';

  constructor(
    private http: Http,
    private authenticationService: AuthenticationService
  ) {}

  get() {
    return Observable
      .fromPromise(this.authenticationService.getAccessToken())
      .flatMap(accessToken => this.synchronize(accessToken));
  }

  synchronize(accessToken) {
    const today = new Date();
    today.setHours(0, 0, 0, 0);
    const todayTimestamp = today.getTime();

    const batch = [{...}];

    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    const body = new URLSearchParams();
    body.append('access_token', accessToken);
    body.append('batch', JSON.stringify(batch));

    return this.http
      .post('https://graph.facebook.com', body.toString(), { headers })
      .map(response => response.json());
  }
}

在发出请求时,我知道为什么会收到此错误?

1 个答案:

答案 0 :(得分:0)

404错误=未找到(https://en.wikipedia.org/wiki/HTTP_404

因此,您尝试访问的资源可能存在错误,或者您的连接存在其他问题。

确实 - 如果我手动尝试从浏览器导航到https://graph.facebook.com,我会收到404错误。

由于这是使用https协议,因此也可能是身份验证问题。