我一直在尝试连接我的后端(spring boot/java)和前端(angular),但我一直在控制台中得到这个错误:
error: SyntaxError: Unexpected token 'S', "[Services {"... is not valid JSON at JSON.parse,
message: "Http failure during parsing for http://localhost:8080/providers/getServices"
我的服务看起来像这样:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { ServiceSection } from 'src/app/models/services/service-section';
@Injectable({
providedIn: 'root'
})
export class HomeItemService {
private servicesUrl: string;
private projectsUrl: string;
// services: Observable<ServiceSection[]>;
constructor(private http: HttpClient) {
this.servicesUrl = 'http://localhost:8080/providers/getServices';
this.projectsUrl = 'http://localhost:8080/projects/getAllProjects'
}
public getAll(): Observable<ServiceSection[]> {
console.log(this);
return this.http.get<ServiceSection[]>(this.servicesUrl);
}
}
我的控制器是这样的:
package hibera.web.api.controllers;
import com.google.api.core.ApiFuture;
import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;
import hibera.web.api.domain.Services;
import hibera.web.api.service.FirebaseInit;
import hibera.web.api.service.ProvidingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
@RestController
@RequestMapping("/providers")
@CrossOrigin(origins = "http://localhost:4200")
public class ProvidersController {
@Autowired
FirebaseInit db;
@Autowired
public ProvidingsService service;
@GetMapping("/getServices")
public String getServices() throws InterruptedException, ExecutionException {
List<Services> services = new ArrayList<Services>();
CollectionReference service = db.getFirebase().collection("services_section");
ApiFuture<QuerySnapshot> querySnapshot = service.get();
for (DocumentSnapshot doc:querySnapshot.get().getDocuments()){
Services serv = doc.toObject(Services.class);
services.add(serv);
}
return services.toString();
}
}
我知道它没有被解析为一个json对象,但是当我尝试添加{responseType: 'text'}
时,它在控制台中给了我一堆错误。在postman中一切都很好,但是尝试将数据从数据库循环到客户端让我非常头痛。老实说,我认为这与API无关,而是与客户端有关。
如果有人能给我答案或者至少帮我。
提前感谢:)
1条答案
按热度按时间h5qlskok1#
Postman 回复: