Pravat Panda
Pravat Panda

Reputation: 1090

jQuery Ajax POST request not hitting Spring controller

I am unable to hit my spring controller using jQUERY AJAX POST. I am trying to fetch a list of string using JSON response.

My javascript function which is called when a dropdown is selected:

function doAjaxPost() {  

  var name = $('#selected_report').val();

  alert("POST Ajax request initiating.... name : " + name);

  $.ajax({  
    type: "POST",  
    url: "/NewILR/interfaceReportsSearch/findEntityTypes", 
    contentType: "application/json",
    data: JSON.stringify({selectedReport:name}),  
    dataType: "text",
    success: function(response){
      if(response.status == "SUCCESS"){
          alert("SUCCESS");
      }else{
            alert("FAILURE");
      }       
    },  
    error: function(e){  
      alert('Error: ' + e);  
    }  
  });  
} 

NOTE Two of the above alerts are displayed (POST Ajax request initiating... and 'Error: ' + e))

Spring Controller to handle the AJAX request:

@RequestMapping (value="/interfaceReportsSearch/findEntityTypes",method=RequestMethod.POST, produces = "application/json")
    public @ResponseBody JsonResponse addUser(@RequestBody String selectedReport , BindingResult result ){

    System.out.println("Ajax Request Received! reportName = " + selectedReport);

NOTE the above print statement is never executed. I also do not see a single console output, maybe the request does not get to the server.

No special beans are added to the servlet condiguration but I have the below two added:

<mvc:annotation-driven />
<context:annotation-config/> 

Following jars are added to my classpath.

jackson-core-2.2.0
jackson-annotations-2.2.0
jackson-databind-2.2.0

UPDATE

Solved: changed the datatype to json

Upvotes: 0

Views: 3430

Answers (1)

Dangling Piyush
Dangling Piyush

Reputation: 3676

Change your url attribute of ajax request by following:

url: "${pageContext.request.contextPath}/NewILR/interfaceReportsSearch/findEntityTypes",

This should do the job for you.

Upvotes: 1

Related Questions