【发布时间】:2020-06-02 20:42:44
【问题描述】:
在将 Spring Boot 应用程序与我的 Angular 应用程序连接时遇到问题。我想从角度执行获取请求。为此,我通过将控制器注释为 @CrossOrigin 对我的 Spring Boot 应用程序进行了更改。 到目前为止,我能够从 angular 中访问 tomcat url,因为 spring boot 中的日志正在正确发布。但是,我无法在我的角度应用程序中获取任何响应。
I believe it is related to type. For simplest type-String, I had tried but no help. I am getting the below error:
```XHR failed loading: GET "http://localhost:8080/home".```
Here is my spring boot:
```
@CrossOrigin
@RestController
public class HomeController {
private CoronavirusDataService coronavirusDataService;
@Autowired
public HomeController(CoronavirusDataService coronavirusDataService)
{
this.coronavirusDataService=coronavirusDataService;
}
@GetMapping("/home")
public String home() throws IOException, InterruptedException, JSONException {
String abc="Ria";
List<LocationStats> locationStatsList= coronavirusDataService.fetchVirusData();
//locationStatsList.stream().forEach(System.out:: println);
//var myObj = JSON.parse('{"p": 5}');
//return locationStatsList.toString();
return abc;
}
}```
Here is my angular:
```import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {CoronaData} from '../model/corona-data';
@Component({
selector: 'app-corona-module',
templateUrl: './corona-module.component.html',
styleUrls: ['./corona-module.component.css']
})
export class CoronaModuleComponent implements OnInit {
coronaCount: CoronaData[] = [];
coronaC: string;
constructor(private http: HttpClient) { }
ngOnInit(): void {
this.getAllCases();
console.log(this.coronaCount);
}
public getAllCases(){
const url = 'http://localhost:8080/home';
this.http.get<string>(url).subscribe(
res => {
this.coronaC = res;
console.log(this.coronaC);
},
error => {
alert('An error has occured');
console.log(this.coronaC);
}
);
}
}```
I am quite struggling with basic, any help in this regard will be appreciated.
Thank you!
【问题讨论】:
-
这是固定的。谢谢!
-
请提供您的问题的答案,以避免人们在阅读本文的同时期待没有解决方案的不必要的努力。
标签: java angular spring spring-boot