简体   繁体   中英

Receiving response as JSON in ajax call in jsp

I have a jsp page that sends response as a json. The page is requested through AJAX call. But the response went into Ajax error part instead of success part.

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers

The following is my jsp page Script:

$("#Student").delegate("select[id='year.id']", "change", function () {
         //alert("hi");
        var id = $(this).attr('id');
        var value=$("select[id='year.id']").val();
        //alert(id);
        //alert(value);
       //console.log($(id).val());
        $("select[id='classs.id']").empty();
        $("select[id='classs.id']").append('<option value="">Select Class</option>');
        $("select[id='section.id']").empty();
        $("select[id='section.id']").append('<option value="">Select Section</option>');
        $.ajax({
            type: 'GET',
            url: 'getStandard',
            contentType: "application/json;charset=utf-8",
            datatype: "Json",
            data: { Year: value },
            success : function(datas) {     
                $.each(datas, function (i, data) {
                    $("select[id='classs.id']").append('<option value="' + data.id + '">' + data.name + '</option>');
                });
            }
        });
        return false;
    });

This Is my server side code where I am getting the response as Student object :

Controller :

        @RequestMapping(value = "/getStudentName", method =  RequestMethod.GET)
public @ResponseBody String getStudentName(@RequestParam("Year") String idYear, @RequestParam("Classs") String idClass, @RequestParam("Section") String idSection) throws JsonProcessingException 
{       
    List<Student> student = userService.getStudentName(idYear,idClass,idSection);       

     return student ;
}

I am new to ajax using via Spring

@RequestParam is a parameter in the uri ( http://uri?year=2018 ). If what you want is JSON then you should use @RequestBody instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM