[英]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) . 因此,我想将对象从javascript发送到控制器。这是我尝试的代码段,但错误400(错误请求) 。
My bean is as follow : (StudentAnswers.java) 我的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
}
My javascript file having ajax call is : 我的具有ajax调用的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);
}
});
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将数据从AJAX发送到Controller,但它不起作用。
JSON.stringify(studentAnswers)
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.
因此,我将javascript对象更改为与bean相同。 So, following is my object and bean which works fine.
因此,以下是我的对象和Bean,它们工作正常。
JavaScript Object : 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
}
Final AJAX call looks like 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);
}
});
Controller Function: 控制器功能:
@RequestMapping(value={"submitTest"},method = RequestMethod.POST)
public ModelAndView submitTest(@RequestBody StudentAnswers studentAnswers)
{
System.out.println(studentAnswers.toString());
return new ModelAndView("student/testBegin");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.