【发布时间】:2020-07-23 15:24:37
【问题描述】:
我正在使用 Angular 和 Spring Boot 构建一个 Web 应用程序。我正在尝试从 localhost 获取数据,但我一无所获。
这是来自服务器的json数据
这是我的服务 ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {map, catchError} from "rxjs/operators";
import { Observable, throwError } from 'rxjs';
import {Entreprise} from '../modules/entreprise';
@Injectable({
providedIn: 'root'
})
export class EntrepriseService {
constructor(private http:HttpClient) { }
public getEntreprises():Observable<Entreprise>{
return this.http.get<Entreprise>("http://localhost:8080/entreprises/all");
}
}
这是组件 ts:
import { Component, OnInit } from '@angular/core';
import { EntrepriseService } from '../services/entreprise.service';
import {Entreprise} from '../modules/entreprise';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
entreprises:Entreprise;
constructor(private entrepriseService:EntrepriseService) { }
public onGetEntreprises(){
return this.entrepriseService.getEntreprises().
subscribe(data =>{
this.entreprises = data;
console.log(data);
});
}
ngOnInit(): void {
}
}
这是html模板
<h4>entreprises</h4>
<button (click)="onGetEntreprises()">get </button>
<p *ngFor="let ee of entreprises">{{ee.content.name}}</p>
这是我点击按钮时遇到的错误
【问题讨论】:
-
你确定你没有使用 https 吗?
-
不,我没有使用 https
-
你有什么尝试调试这个问题。我看不出有什么问题。但为了调试,请使用纯 JavaScript 文件并使用如下所示的 fetch 请求: fetch("localhost:8080/entreprises/all") .then((response) => response.json()) .then((data) => console。日志(数据))
标签: java json angular spring spring-boot