0

My controller is returning an array. But I cannot get the values of this array when ajax is success. How can I fix it?

Controller

@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 call:

$(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]);
                    }
                });
            }
        });
    });
});

Edited: text/plain as application/json

Volkan Şahin
  • 1,181
  • 2
  • 10
  • 15

0 Answers0