简体   繁体   English

在jsp中的ajax调用中将响应接收为JSON

[英]Receiving response as JSON in ajax call in jsp

I have a jsp page that sends response as a json. 我有一个jsp页面,将响应作为json发送。 The page is requested through AJAX call. 该页面是通过AJAX调用请求的。 But the response went into Ajax error part instead of success part. 但是响应进入Ajax错误部分而不是成功部分。

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: 以下是我的jsp页面脚本:

$("#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 : 这是我的服务器端代码,在其中我将响应作为Student对象获取:

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 我是通过Spring使用Ajax的新手

@RequestParam is a parameter in the uri ( http://uri?year=2018 ). @RequestParam是uri中的参数( http:// uri?year = 2018 )。 If what you want is JSON then you should use @RequestBody instead 如果您想要的是JSON,则应改用@RequestBody

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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