简体   繁体   English

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

[英]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.

相关问题 将模型属性从Jquery Ajax调用传递到Controller方法 - Passing Model property from Jquery Ajax call to Controller method 通过 ajax 调用将复杂的 model 传递到 controller 时遇到问题 - Having trouble passing complex model to controller through ajax call 将数组从 ajax 调用传递给 controller,controller 中的数组为空 - passing an array from ajax call to controller, array empty in controller MVC 6使用ajax将模型从视图传递到控制器到部分视图 - MVC 6 passing a model from view to controller to partial view using ajax Ajax调用未通过Url.Content从视图传递“&”到控制器 - Ajax call is not passing “&” to the controller from view through Url.Content 将属性从Backbone模型传递给Spring Controller作为参数 - Passing attributes from Backbone model to Spring Controller as arguments 如何从JQuery数据表AJAX调用Spring控制器函数 - How to call Spring controller function from JQuery Data tables AJAX 如何显示从AJAX调用Spring控制器方法的响应? - How to show response from AJAX call to Spring controller method? Ajax 后(模型绑定)将 null 传递到 controller - Ajax Post (Model binded) passing null to the controller Ajax调用将空数组传递给控制器 - ajax call passing empty array to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM