I am trying to make an ajax call using jquery in my spring MVC based application.
Below is my ajax controller
@Controller
@RequestMapping("/ajax/*")
public class AjaxController extends BaseController {
private static Logger log = Logger.getLogger(AjaxController.class);
@Autowired
private ICountryService countryService;
@RequestMapping(value = "/findCountriesByRegionId", method = RequestMethod.GET)
public @ResponseBody
List<Country> findCountriesByRegionId(
@RequestParam(value = "regionId") int regionId) {
log.info("finding countries by region id [" + regionId + "]");
List<Country> countryList = countryService.findByRegionId(regionId);
return countryList;
}
and Below is my javascript code
function populateCountriesByRegionId(regionId) {
alert("11");
$.getJSON("ajax/findCountriesByRegionId", {
regionId : regionId
}, function(countryList) {
alert("2");
$("#countryId").empty();
// $("#countryId").html("");
var options = $("#countryId");
options.append($("<option />").val('0').text(""));
$.each(countryList, function() {
options.append($("<option />").val(this.countryId).text(
this.countryName));
});
});
}
But my controller method is not getting called at all.
When i am on page with url like http://localhost/myApp/emp/new and my ajax url is like ajax/findCountriesByRegionId it gives me error saying no mapping for url myapp/emp/ajax/findCountriesByRegionId.
Why its checking for url myapp/emp/ajax/findCountriesByRegionId. It should be myapp/ajax/findCountriesByRegionId
When I sue url like /ajax/findCountriesByRegionId (added / at the begining) , nothing happens. No error at all. No conroller invoked.
I wanted to put all my ajax methods in one controller and call those while executing other controller like emp in this case.
Please help.
$.getJSON("/myApp/ajax/findCountriesByRegionId",/ajax.