【问题标题】:"Http failure during parsing for http://localhost:8080/providers/getServices"\"解析 http://localhost:8080/providers/getServices 期间的 Http 失败\"
【发布时间】:2023-02-21 23:44:47
【问题描述】:

我一直在尝试将我的后端(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'} 时,它在控制台中出现了一堆错误。在邮递员中一切正常,但试图将数据从数据库循环到客户端让我很头疼。老实说,我认为它与 API 无关,而与客户端有关。

我有人可以为我解答或至少帮助我。

提前致谢 :)

【问题讨论】:

  • 您是否尝试过用连字符或下划线代替驼峰式拼写作为 URI 中的单词定界符?
  • 你能添加任何邮递员返回的呼叫localhost:8080/providers/getServices吗?
  • @JohnWilliams 我刚刚发布了邮递员的回电作为答复!

标签: java json angular typescript spring-boot


【解决方案1】:

邮递员返回这个:

    }, Services {name = Process Automation, img_url = drawings/process_automations.svg, anchor tag = automation, description = test
    }, Services {name = Maintenance, img_url = drawings/maintenance.svg, anchor tag = maintenance, description = test
    }, Services {name = Web Design, img_url = drawings/web_design.svg, anchor tag = design, description = test
    }, Services {name = Hosting, img_url = drawings/hosting.svg, anchor tag = hosting, description = test
    }, Services {name = E-commerce, img_url = drawings/ecommerce.svg, anchor tag = ecommerce, description = test
    }
]

【讨论】:

  • 您应该编辑原始帖子以包含此部分,而不是添加答案,否则它将被删除为“不是答案”
猜你喜欢
  • 1970-01-01
  • 2020-10-13
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多