0

I am trying to get the list of objects from controller to view through ajax request.I am getting 406 error while I am trying this.

Failed to load resource: the server responded with a status of 406 ()

My controller is as follows,

@RequestMapping("/getReservationList")  
public @ResponseBody List<Reservation> getReservationList()  
{  
HttpHeaders headers = new HttpHeaders();
List<Reservation> rlist=service.showResults();
System.out.println("size is "+rlist.size());
headers.add("Content-Type", "application/json");
System.out.println("inside controller");

//model.addAttribute("list", rlist);

return rlist;  
 } 

My Jsp page is as follows,

<!DOCTYPE html> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<html> 
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#buttona").click(function(){alert("hell");
$.ajax({url: "getReservationList", success: function(result){
    for (var i in result){
          // data[i].something, etcale
          alert(result[i]);
         }
  $("#div1").html(result);
}});
});
});
</script>
</head> 
 <body>  
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

 <input id="buttona" type="button" value="Register" >
 
 </body>  
 </html>
Srinath Murugula
  • 560
  • 1
  • 8
  • 30
  • Hi, you are returning json or simply string ? – Swati Mar 07 '21 at 11:33
  • I am returning List of objects right? – Srinath Murugula Mar 07 '21 at 12:28
  • 14000 reputation you are really awesome,please let me know how to solve this?Should I convert it into JsonResponse. – Srinath Murugula Mar 07 '21 at 12:30
  • 1
    remove this `headers.add("Content-Type", "application/json");` line see what happens . Also your ajax looks ok to me but if you are returning json then you need to parse it or use `dataType : "json"` . – Swati Mar 07 '21 at 12:41
  • ok I will do it.Actually I am looking for job.I am 2 years experienced in java.Swati if you know that there is any vacancy in some or other company.will you recommend me?My facebook ID is https://www.facebook.com/munna.srinath/ – Srinath Murugula Mar 07 '21 at 16:15
  • I tried removing the header.I am getting same error.-->Failed to load resource: the server responded with a status of 406 () – Srinath Murugula Mar 07 '21 at 16:28
  • Hi, see if [this](https://stackoverflow.com/questions/16335591/spring-mvc-json-406-not-acceptable) might help . – Swati Mar 08 '21 at 04:37
  • After adding few jars (jackson mapper asl),now I am not getting that error. – Srinath Murugula Mar 08 '21 at 14:45

0 Answers0