Link to home
Start Free TrialLog in
Avatar of Massimo Scola
Massimo ScolaFlag for Switzerland

asked on

jQuery and Spring MVC: Problems when using Ajax POST

I have written my first Spring MVC controller. While I have no problems with GET requests, I am still having some issues with POST methods.

This is the code in my controller:
@RequestMapping(value = "/editorsWriters", method = RequestMethod.POST)
    public <HttpServletRequest> ResponseEntity<List<FreelancerContactWriterListView>> getEditorsWriters(
                                                                    @RequestParam int[] languageIds, 
                                                                    @RequestParam int[] summaryGroupIds, 
                                                                    @RequestParam String[] writerStates,
                                                                    HttpServletRequest request, HttpServletResponse response) {

        WriterState[] writerState = Arrays.copyOf(writerStates, writerStates.length, WriterState[].class);
        

        List<FreelancerContactWriterListView> writers = writerService.getActiveWriters(languageIds, summaryGroupIds, writerState)
                                                                    .stream()
                                                                    .map(writer -> this.convertTo(writer))
                                                                    .collect(Collectors.toList());

        
           return new ResponseEntity<List<FreelancerContactWriterListView>>(writers, HttpStatus.OK);
    }

Open in new window


and this is the JavaScript code:

 $("#search").on("click", function(e) {
            var languages=$("#languages").val();
            var summaryGroups = $("#summaryGroups").val();
            var writerStates = $("#writerState").val();
            
            var myObject = new Object();
            myObject.languageIds = languages;
            myObject.summaryGroupIds = summaryGroups;
            myObject.writerStates = writerStates;

            var myData = JSON.stringify(myObject);

            console.log(myData);

            $.ajax({
                method: 'POST',
                dataType: 'json',
                data: myData,
                url:'/api/pps/contactFreelancers/editorsWriters',
                contentType: "application/json"
            })
            
        });

Open in new window


This is the JSON sent:

{"languageIds":["1"],"summaryGroupIds":["11"],"writerStates":["PROBATION","REGULAR"]}

Open in new window


The problem is that I get a POST 404 (Not Found) error in the JavaScript console
POST https://dev1.getabstract.com/api/pps/contactFreelancers/editorsWriters 404 (Not Found)

and the following message in the terminal of my IDE:

2019-07-14 22:49:06,923 DEBUG [BaseControllerExceptionHandler] Page not found for the URL https://dev1.getabstract.com/api/pps/contactFreelancers/editorsWriters with query null: Required int[] parameter 'languageIds' is not present

Open in new window



So it is reaching or sending the POST to the correct URL, but what am I doing wrong?

Is the way I have created the data wrong?
languages, summaryGroups and writersStates are all arrays and I am putting them together into one JSON.

Thanks for your help

Massimo
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
for testing, try to replace :
myObject.languageIds = languages;
by :
myObject.languageIds = [1];

and another test, replace :
var myData = JSON.stringify(myObject);
by :
var myData = myObject;
Avatar of Massimo Scola

ASKER

The problem was a type issue: string/integer

I solved this problem by putting the validation into a bean.

Thanks a lot!