如何在AJAX中访问使用@ResponseBody返回的数组?

时间:2017-03-04 16:17:49

标签: ajax spring jsp post

我的控制器正在返回一个数组。但是当ajax成功时,我无法获得这个数组的值。我该如何解决?

控制器

@ResponseBody
@RequestMapping(value="/practice/{category}/option", method = RequestMethod.POST, 
produces = "application/json; charset=UTF-8")
    public String[] practicePageOption(@PathVariable("category") String category, @RequestParam("index") int index, ModelMap model, HttpSession session){

    String[] array = new String[5];
    for (int i = 0; i < array.length; i++) {
        array[i] = optionsList.get(index).get(i).get("meaning").toString();
    }

    return array;
}

ajax电话:

$(document).ready(function() {                        
    $('#pass').click(function(event) {
        var inputIndex = $('#index').val();
        $.ajax({
            type: "POST",
            url: "${pageContext.request.contextPath}/practice/${category}",
            async:false,
            data: { index: inputIndex },
            success: function(data){
                $('#label').text(data);
                $.ajax({
                    type: "POST",
                    url: "${pageContext.request.contextPath}/practice/${category}/option",
                    async:false,
                    contentType:"application/json; charset=utf-8",
                    dataType:"json",
                    data: { index: inputIndex },
                    success: function (resp) {
                        $('#optionA').text(resp[0]);
                    }
                });
            }
        });
    });
});

编辑: text / plain as application / json

0 个答案:

没有答案