3

I need to send data to spring mvc controller from view using jquery. Spring mvc will insert/update these values in db. I am using below code

JQuery:

 var json = {"KPI" : visual, "partnerName": params['partnerName'],
 "partnerAddress" : params['partnerAddress'], "documentType" : params['documentType'], 
 "itemQualifier": getItemQualifier(),"itemIdentifiers" : params['itemIdentifiers'], 
 "currency" : getCurrency(), "sellSide": getSellSide()};

 $.ajax({                                                                   
        type: "POST",
        url: "/reports/savefilters",
        data: json,
        success: function(response){
                 alert('success: ' + response);
                },
        error: function(e){ 
                 alert('Error: ' + e);
                }
 });

Spring MVC controller Code:

@RequestMapping(value="/savefilters", method=RequestMethod.POST)
public String saveFilters(@RequestBody AnalyticsFilters filters){
    System.out.println("savefilters method in controller");
    System.out.println("KPI:"+filters.getKPI());
    if(filters.getPartnerName() != null && filters.getPartnerName().size()>0 ) System.out.println("partnerName:"+filters.getPartnerName().get(0));
    if(filters.getPartnerAddress() != null && filters.getPartnerAddress().size()>0 ) System.out.println("partnerAddress:"+filters.getPartnerAddress().get(0));
    if(filters.getDocumentType() != null && filters.getDocumentType().size()>0 ) System.out.println("documentType:"+filters.getDocumentType().get(0));
    System.out.println("itemQualifier:"+filters.getItemQualifier());
    if(filters.getItemIdentifiers() != null && filters.getItemIdentifiers().size()>0 ) System.out.println("itemIdentifiers:"+filters.getItemIdentifiers().get(0));
    System.out.println("currency:"+filters.getCurrency());
    System.out.println("sellSide:"+filters.getSellSide());
    new DashboardDisplayService().saveFilters();
    return "successful";
}

Post request is getting converted to Get method and not able to reach spring mvc controller. I tried with different ajax json options like datatype,contenttype etc. how to make sure POST request doesn't change to GET method???

9
  • Have you verified through network inspection that a GET is being sent, or are you deducing this from the fact that you are not reaching your controller? How is the ajax request triggered, could there be a default action that is not prevented, such as a form submit? Commented Jul 5, 2016 at 9:31
  • 1. Yes. I did inspect element. I could see "405 - GET is not supported error" when I opened the link in new tab. 2. I have jquery dialog with options to choose. I am trying to update the db by sending jquery ajax request to spring mvc. There is no form submit is involved here Commented Jul 5, 2016 at 9:35
  • When you open the link in a new tab that will always be a GET. Did you see the actual AJAX request in your network inspection tools? Commented Jul 5, 2016 at 9:36
  • yeah @David Hedlund. I checked just now in network tab. I can see "POST" method in headers. Looks its not problem with Request method Commented Jul 5, 2016 at 9:51
  • Alright, at least that's progress. Does the URL look alright in the network tab? What does the response say? Commented Jul 5, 2016 at 10:04

1 Answer 1

0

Ajax method returns the messages in ResponseBody. I have modified your handler method as below.

@RequestMapping(value="/savefilters", method=RequestMethod.POST)
public @ResponseBody String saveFilters(@RequestBody AnalyticsFilters filters){
...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the comment Mihir. As I mentioned in above comments, its expecting string parameter which is missing. I need to look into code about this why its occuring.
Is it reaching inside your handler method ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.