简体   繁体   中英

Passing model from ajax call to spring controller

I read many articles describing this and I tried to use the same mechanism but still it does not work for me.

So, I want to send the object from javascript to my controller.Here is the snippet I tried but error 400(Bad request) .

My bean is as follow : (StudentAnswers.java)

public class StudentAnswers {
    private Integer testId;
    private Integer studentId;
    private String[] questionIds;
    private String[] answerIds;
..
//all get and set method to access this
}

My javascript file having ajax call is :

function submitTest()
{
    var test_id = 1;
    var student_id = 1;
    var q = [];
    q[0] = "question 1";
    q[1] = "question 2";
    var a = [];
    a[0] = "answer 1";
    a[1] = "answer 2";
    var studentAnswers = {
           "testId": test_id,
           "studentId": student_id,
           "questionIds": q,
           "answerIds" : a
        };    

    $.ajax({
    type : "POST",
    url : "submitTest",
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    data : studentAnswers,
    success : function(response) {
       alert('test submitted');
    },
    error : function(e) {
       alert('Error: ' + e);
    }
});

Finally, my controller function is :

@RequestMapping(value={"submitTest"},method = RequestMethod.POST)
public ModelAndView submitTest(@RequestBody StudentAnswers studentAnswers)
{
    ...
    return new ModelAndView("student/testBegin");
}

尝试对发布数据进行stringify如下所示:

data : JSON.stringify(studentAnswers)
var studentAnswers  = new Object();

studentAnswers.testId = "11";
studentAnswers.studentId = "12345"; 
studentAnswers.questionIds =  "secret"; 
studentAnswers.answerIds ="111";    

$.ajax({

    url: urlSelected,
    type: "POST",
    dataType: "json",
    data: JSON.stringify(studentAnswers),
    contentType: "application/json",
    mimeType: "application/json",       
    success: function (result) {
        if (result.success) {
           alert(result.message);
        } else {
            alert(result.message)
        }
    },
    error:function(error) {
        alert(error.message);                       
    }
});

Thank you guys for the help. I tried using JSON.stringify to send the data from AJAX to Controller and it did not work.

JSON.stringify(studentAnswers)

However, when I took a look at the data that I am sending and compare with the type of data expected, I found out that there was a type mismatch. So, I changed my javascript Object to be identical to my bean. So, following is my object and bean which works fine.

JavaScript Object :

var studentAnswers  = new Object();
studentAnswers.testId = 1;
studentAnswers.studentId = 1; 
studentAnswers.questionIds =  new Array();
studentAnswers.questionIds[0] = "12345";
studentAnswers.answerIds =new Array();
studentAnswers.answerIds[0] = "12345";

Bean :

public class StudentAnswers {
    private Integer testId;
    private Integer studentId;
    private String[] questionIds;
    private String[] answerIds;
..
//all get and set method to access this
}

Final AJAX call looks like this :

$.ajax({
    type : "POST",
    url : "submitTest",
    dataType: "json",
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    data : JSON.stringify(studentAnswers),
    success : function(response) {
       alert('test submitted');
    },
    error : function(e) {
       alert(e.message);   
    }
}); 

Controller Function:

@RequestMapping(value={"submitTest"},method = RequestMethod.POST)
public ModelAndView submitTest(@RequestBody StudentAnswers studentAnswers)
{
    System.out.println(studentAnswers.toString());
    return new ModelAndView("student/testBegin");
}

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