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???

Hemachandra
  • 69
  • 2
  • 14
  • 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? – David Hedlund Jul 05 '16 at 09: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 – Hemachandra Jul 05 '16 at 09: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? – David Hedlund Jul 05 '16 at 09: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 – Hemachandra Jul 05 '16 at 09:51
  • Alright, at least that's progress. Does the URL look alright in the network tab? What does the response say? – David Hedlund Jul 05 '16 at 10:04
  • Response is having error report but preview shows "400 - Required String parameter 'page_redirect' is not present" error. – Hemachandra Jul 05 '16 at 10:06
  • Well there's your error, then. The action you are reaching is expecting a parameter called `page_redirect` and you're not passing any. Again, does the URL look alright in the request body? Are you familiar with a `page_redirect` parameter in your code? – David Hedlund Jul 05 '16 at 10:10
  • yeah David. Url looks good. I need to check about page_redirect and why its giving error. – Hemachandra Jul 05 '16 at 10:12
  • Thanks for the reply. By the way, I didn't downvote @10086 – Hemachandra Jul 05 '16 at 10:25

1 Answers1

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){
...
}
Mihir Gohel
  • 316
  • 2
  • 6