接受另一个保护人的请求不起作用

时间:2019-04-22 11:10:37

标签: angular

我读了它:angular-cli server - how to proxy API requests to another server?

但是该解决方案对我不起作用。

Angular在localhost:4200上工作,变量msg应该从localhost:8080/msg中获取。

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'netty-board-frontend';
  msg = 'not found msg';
  constructor(private http: HttpClient) {}

  ngOnInit(): void {
    this.http.get('/api/msg', {responseType: 'text'}).subscribe(data => {
      this.msg = data as string;
    });
  }
}

我已创建proxy.conf.json文件:

  {
    "/api": {
      "target": "http://localhost:8080",
      "secure": false
    }
  }

并放入package.json

    "start": "ng serve --proxy-config proxy.config.json",

但变量msg仍来自http://localhost:4200/api/msg而不是http://localhost:8080/msg

Request URL: http://localhost:4200/api/msg
Request Method: GET
Status Code: 404 Not Found
Remote Address: 127.0.0.1:4200

0 个答案:

没有答案