【问题标题】:Override base URL on angular httpclient get request在 Angular httpclient 获取请求上覆盖基本 URL
【发布时间】:2018-12-03 21:50:37
【问题描述】:

我正在尝试向某个 URL 发出 get 请求,但它似乎被替换为另一个,这导致向不存在的 URL 发出 get 请求。

这是我的代码

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';


@Injectable()
export class ApitestProvider {
  tests;
  apiUrl = "http//:localhost:8000/api";
  testAdded;
  testGotten;
  testDeleted;
  constructor(public http: HttpClient) {

    console.log('Hello ApitestProvider Provider');
  }

  getTests() {
    this.http.get(this.apiUrl + "/tests").subscribe(data => {
      console.log(data);
      this.testGotten = data;
    }), err => {
      console.log(err);
    }

  }

但它最终会请求以下 URL

url: "http://localhost:8100/http//:localhost:8000/api/tests"

提前致谢。

【问题讨论】:

  • 你在使用拦截器吗?
  • 您可以在index.html 文件中检查您的基本网址

标签: angular http ionic-framework get request


【解决方案1】:

您输入了错误的 API URL。

而不是这个:apiUrl = 'http//:localhost:8000/api';

应该是这样的:apiUrl= 'http://localhost:8000/api';

Stackblitz

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    相关资源
    最近更新 更多