我正在Angular7中处理“搜索表单”方案。我使用了HTTP POST插入的http GET,尽管它需要通过使用angular作为前端和spring-boot作为后端从mysql数据库中获取json对象数组。所有后端部分均已实现,由于我使用了Http POST请求,因此我无法将这些数据传输到前端。
我也尝试使用Http GET。但是我需要传递一个json嵌套对象。所以我用了POST。
这是我的Spring Boot后端代码:
SearchOutput[] search_output;
@PostMapping(value="/service")
@CrossOrigin(origins = "http://localhost:4200",allowedHeaders = "*")
public ArrayList<SearchOutput> SearchContract(@RequestBody SearchContract search_input) throws InterruptedException {
rooms_adults[] rooms_adults2 = search_input.getRooms_adults();
ArrayList<SearchOutput> OutputArray = new ArrayList<SearchOutput>();
SearchOutput singleOutput;
List<Integer> hotel_ids ;
for (rooms_adults r_a : rooms_adults2) {
hotel_ids= trueContdao.getHotelId(search_input.getCheck_in_date(), search_input.getCheck_out_date());
for (int id:hotel_ids){
List<Room_Type> roomTypes = roomTypedao.getHotelRoomType(id,r_a.getTotal_rooms(), r_a.getTotal_adults());
for(Room_Type room:roomTypes){
singleOutput=new SearchOutput(hotelContdao.getHotelName(room.getHotel_id()),room.getRoom_type(), (float) (room.getPrice()*search_input.getTotal_nights()*r_a.getTotal_adults()*r_a.getTotal_rooms()*1.15));
OutputArray.add(singleOutput);
}
}
}
return OutputArray;
}
我的前端代码是
import { Injectable } from '@angular/core';
import { HttpClient ,HttpErrorResponse, HttpHeaders} from '@angular/common/http';
import { SearchOutput } from './SearchOutput.model';
@Injectable({
providedIn: 'root'
})
export class SearchService {
uri = 'http://localhost:9090/tickets';
searchOut: SearchOutput[]
reqHeader = new HttpHeaders({ 'Content-Type': 'application/json','No-Auth':'True' });
constructor(private http: HttpClient) { }
postSearch(SearchData){
return this.http.post<any>(this.uri+'/service',SearchData);
}
}
SearchOutput.model.ts是:
export class SearchOutput {
hotel_name: string;
room_type: string;
price: Float32Array;
}
请帮助。预先感谢
答案 0 :(得分:1)
1)订阅请求
2)将请求标头作为第三个参数传递到httpOptions中,请参阅http
3)使用Proxy.config启用cors,以获取托管在不同主机(HttpVerb +主机名+端口)上的服务的响应,请参考Proxy Configuration