无法执行'打开' on' XMLHttpRequest':无效的网址

时间:2017-03-23 08:33:46

标签: angular ionic2 mean

我遇到了运行时错误:

  

无法执行'打开' on' XMLHttpRequest':无效的网址       错误:无法执行'打开' on' XMLHttpRequest':无效的网址           在http://localhost:8100/build/polyfills.js:3:2793           在XMLHttpRequest.open(eval at s(http://localhost:8100/build/polyfills.js:2:29793),:3:31)

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class Reviews {


  data: any;

  constructor(public http: Http) {
    this.data = null;
  }

  getReviews(){

    if (this.data) {
      return Promise.resolve(this.data);
    }

    return new Promise(resolve => {

      this.http.get('http://localhost:app/api/reviews')
        .map(res => res.json())
        .subscribe(data => {
          this.data = data;
          resolve(this.data);
        });
    });

  }

  createReview(review){

    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    this.http.post('http://localhost:app/api/reviews', JSON.stringify(review), {headers: headers})
      .subscribe(res => {
        console.log(res.json());
      });

  }

  deleteReview(id){

    this.http.delete('http://localhost:app/api/reviews/' + id).subscribe((res) => {
      console.log(res.json());
    });    

  }

}

1 个答案:

答案 0 :(得分:12)

http://localhost:app/api/reviews

这是一个无效的网址(就像错误信息所示)。

如果主机名后面有:,则必须后跟NUMBER端口(然后是路径)。 app不是数字,而是三个字母。