将模型从ajax调用传递给spring控制器

时间:2016-12-05 22:20:56

标签: javascript ajax spring-mvc

我读了许多描述这个的文章,我尝试使用相同的机制,但它仍然不适合我。

所以,我想将对象从javascript发送到我的控制器。这是我试过的片段,但错误 400(错误请求)

我的bean如下:(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
}

我的javascript调用的javascript文件是:

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);
    }
});

最后,我的控制器功能是:

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

3 个答案:

答案 0 :(得分:0)

尝试int *a = (int *)malloc(sizeof(int)); // a is a pointer, its value is the address on the heap *a = 4; // store the value 4 to the address on the heap free(a); // you must free the memory. 发布如下数据:

stringify

答案 1 :(得分:0)

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);                       
    }
});

答案 2 :(得分:0)

谢谢你们的帮助。 我尝试使用 JSON.stringify 将数据从AJAX发送到Controller,但它不起作用。

  

JSON.stringify(studentAnswers)

然而,当我查看我发送的数据并与预期的数据类型进行比较时,我发现存在类型不匹配。 所以,我将我的javascript对象改为与我的bean相同。所以,以下是我的对象和bean工作正常。

JavaScript对象:

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
}

最终 AJAX 调用如下所示:

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

控制器功能:

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