【问题标题】:Failed to execute 'open' on 'XMLHttpRequest': Invalid URL无法在“XMLHttpRequest”上执行“打开”:无效的 URL
【发布时间】:2017-08-15 16:45:24
【问题描述】:

我得到了运行时错误:

无法对“XMLHttpRequest”执行“打开”:无效的 URL 错误:无法在“XMLHttpRequest”上执行“打开”:无效的 URL 在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());
    });    

  }

}

【问题讨论】:

    标签: angular ionic2 mean


    【解决方案1】:
    http://localhost:app/api/reviews
    

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

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

    【讨论】:

      【解决方案2】:

      问题在于您的网址。正确阅读错误消息,它说 URL 无效

      在这里,您在 URL 'http://localhost:app/api/reviews' 中使用了 localhost:app,这是不正确的。您必须使用某种开放端口号而不是随机文本。例如30004000500080808000

      所以你的网址会是这样的'http://localhost:3000/api/reviews'

      【讨论】:

        【解决方案3】:

        就我而言,我在 reactjs 中遇到了同样的问题。我发现端点(http://:ip)中有 typo。所以在纠正之后。问题已解决

        【讨论】:

          猜你喜欢
          • 2017-07-16
          • 2019-12-01
          • 2020-07-18
          • 1970-01-01
          • 2019-04-27
          • 2019-09-25
          • 2015-12-28
          • 2019-04-23
          • 2014-10-17
          相关资源
          最近更新 更多