angular - 无法在 'open' : Invalid URL 上执行 'XMLHttpRequest'

标签 angular ionic2 mean

我遇到了运行时错误:

Failed to execute 'open' on 'XMLHttpRequest': Invalid URL Error: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL at http://localhost:8100/build/polyfills.js:3:2793 at 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());
    });    

  }

}

最佳答案

http://localhost:app/api/reviews

这是一个无效的 URL(就像错误消息所说的那样)。

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

关于angular - 无法在 'open' : Invalid URL 上执行 'XMLHttpRequest',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42970807/

相关文章:

angular - Ionic slides - 前后动态添加幻灯片

c# - 奇怪的数字错误

angular - vscode : manually recheck for errors/problems (angular 2)

javascript - ionic 2中的 native facebook登录

javascript - 在 Angular 应用程序中打开的菜单中单击时防止 md-menu 关闭

angular - Ionic 3 导入自定义组件

python - Sklearn PCA,如何恢复较低维度的均值?

python - 为什么 skimage 均值过滤器不适用于 float 组?

angular - Angular 2/4 中的嵌套路由

angular - 如何在 Angular 2 中开发与 app 模块并行的多个模块?