I am creating a spring boot application to book flights. I have defined a controller to retrieve flight details from database:
@Controller
@RequestMapping("/home")
public class FlightController {
@Autowired
private FlightService flightService;
@GetMapping("/search")
public String getFlights(
@RequestParam("origin") String origin,
@RequestParam("destination") String destination,
Model model,
HttpSession session) {
List<Flight> flightList = flightService.getFightsByOriginAndDestination(origin, destination);
if(flightList.isEmpty()) {
model.addAttribute("message", "No flights available for the given origin and destination.");
return "home";
}
else {
model.addAttribute("flightList", flightList);
model.addAttribute("username", session.getAttribute("username"));
return "home";
}
}
}
Now, I want do display the details in a kendo grid by making an ajax call. I tried something like this, but it didn’t work:
$(document).ready(function () {
$.ajax({
url: "/home/search",
type: "GET",
dataType: "local",
success: function (data) {
$("#grid").kendoGrid({
dataSource: {
data: data,
pageSize: 10
},
height: 550,
sortable: true,
pageable: true,
columns: [
{ field: "flight_Number", title: "Flight Number" },
{ field: "airline_Name", title: "Airline Name" },
{ field: "departure", title: "Departure" },
{ field: "arrival", title: "Arrival" },
{ field: "date", title: "Date" },
{ field: "price", title: "Price" }
]
});
},
error: function (xhr, status, error) {
console.error("Failed to fetch flight data:", error);
}
});
});
I know something is wrong, but I am unable to point out the issue. Can someone help?
New contributor
Hisham Gundagi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.