My Spring-boot POST endpoint returns data that does not work with the type ahead plugin I am using. It works when I use GET.
This GET endpoint works fine:
@RequestMapping(value = "/station", method = RequestMethod.GET)
public @ResponseBody List<Station> getstation() {
List<Station> listStation = stationService.findAll();
return listStation;
}
with this javascript:
$.get("/station", function(data) {
console.log(data);
$("[name='query']").typeahead({
source: data,
minLength: 3
});
}, 'json');
The data returned looks like [{id:123,name:"ABC"}]
.
If I try using POST endpoint:
@RequestMapping(value = "/findstation", method = RequestMethod.POST)
public @ResponseBody List<Station> findstation(@RequestBody Station jsonSearchString) {
List<Station> listStation = stationService.stationContaining(jsonSearchString.getName());
return listStation;
}
with javascript:
$('#queryStation').keyup(function() {
console.log("in change function statoion oc");
var stationName = $(this).val();
if(stationName.length==3){
console.log("the length statement is true");
ajax_search(stationName);
}
});
function ajax_search(stationName){
console.log("search function value " +stationName);
var stationJson = '{ "name":"' +stationName+ '"}'
$.ajax ({
url: "/findstation",
type: "POST",
data: stationJson,
dataType: "json",
contentType: "application/json;",
success: function(data){
console.log("inside success handler");
stationTypeahead(data);
}
});
}
function stationTypeahead(data){
console.log(data);
$('#queryStation').typeahead({
source: data
});
}
Returns JSON like [{id:123, name:"LAX"}]
- which does not seem to work with the plugin. typeof data;
returns object.
If I hard code, for example data = [{"id":123,"name":"ABC"}]
this works with the plugin.
I am pretty sure the HTML is fine as it works with GET.
What am I missing?
UPDATE
Typeof is object
for both POST and GET.