gpt4 book ai didi

java - 错误: XHR failed loading: GET "http://localhost:8080/home" in agular

转载 作者:行者123 更新时间:2023-12-01 16:25:18 25 4
gpt4 key购买 nike

我在将 Spring Boot 应用程序与我的 Angular 应用程序连接时遇到问题。我想执行来自 Angular 获取请求。为此,我在 Spring Boot 应用程序中进行了更改,将 Controller 注释为 @CrossOrigin。 到目前为止,我能够从 Angular 访问 tomcat url,因为 Spring Boot 中的日志已正确发布。但是,我无法在我的 Angular 应用程序中获取任何响应。

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!

最佳答案

通过以下更改修改我的 Angular 函数:

 public getAllCases(){
const url = 'http://localhost:8080/home';
this.http.get(url, {responseType: 'text' }).subscribe(
res => {
this.coronaC = res;
console.log(this.coronaC);
},
error => {
alert('An error has occured');
console.log(this.coronaC);
}
);

}```

关于java - 错误: XHR failed loading: GET "http://localhost:8080/home" in agular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62161086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com