我如何将数据从Angular前端传递到spring boot后端?



我有一个spring启动代码,它需要2个参数来执行一些计算并返回一些值。我在浏览器中测试了它,API工作正常。现在我正在尝试将我的Spring启动API连接到Angular。我如何将用户输入(2个参数)发送到spring boot后端,处理数据并将结果返回给Angular以在前端显示它们?

retrieveData(currency:string, amount: number){
return this.http.get<convert[]>. 
(`http://localhost:8080/convert/${currency}/${amount}`);
}

上面你可以在Angular中找到我的方法来检索数据。参数将由用户在UI中提供。我该何去何从?

试一下

In yourcomponent.tsInonsubmit()

this.abcService.abcreg(this.form.value) 

你的abcService.ts文件应该是这样的。

#Imports 

@Injectable({
providedIn: 'root'
})
export class abcService {
constructor(private http: HttpClient,
private httpClient: HttpClient) { }
httpOptions = {
headers: new HttpHeaders({
'Content-Type':'application/json'
})
}
abcreg(abc:abcreg):Observable<any>{
const uploadData = new FormData();
uploadData.append('requestparam1',abc.Name);
uploadData.append('requestparam2',abc.Address);
return this.httpClient.post(environment.api_url+'save_data', uploadData)
}

最新更新