将多个数据变量以简单的方式传递给AJAX函数?

时间:2016-02-10 13:17:14

标签: javascript jquery json ajax spring-mvc

我有大约15到20个数据变量,通过ajax函数下面。

有没有最简单的方法从ajax传递多个变量并以简单的方式在Controller类下面获取那些多个变量?

Jsp页面:

(j/query db-map ["SELECT * FROM table"])

控制器类:

function getAssignedParticularTable(){
        $.ajax({
            type: "POST",
            url: "assignedParticularTable.html",
            data: "operation=assignedParticularTable",
            success: function(response){
                var obj = JSON.parse(response);
                $("#table2").html("");
                var tr="";
                tr+="<thead><tr><th>Sl no.</th><th>Fee Particular Name</th><th>Amount</th><th>Selection</th></tr></thead><tbody>";
                for (var i = 0, j=0; i < obj.length; i++){
                      tr+="<tr>";
                      tr+="<td>" + (++j) + "</td>";
                      tr+="<td>" + obj[i].Name + "</td>";
                      tr+="<td><input type='text' id='amount' name='amount' value='0'/></td>";
                      tr+="<td><input type='checkbox' id='check' name='fee_particular_ids' value='"+obj[i].Id+"' /></td>";
                      tr+="</tr>";
                }
                $("#table2").append(tr);
            }
        });
    }

2 个答案:

答案 0 :(得分:0)

JSP页面

    function getAssignedParticularTable(){
        $('#result2').hide();   
        var jsonValues = {paymentType : $('#paymentType').val(), subPaymentType : $('#subPaymentType').val()};
        $.ajax({
            type: "POST",
            url: "assignedParticularTable.html",
            contentType : 'application/json; charset=utf-8',
            data: JSON.stringify(jsonValues),
            success: function(response){

            }
        });
    }

控制器类:

@RequestMapping(value = "/assignedParticularTable.html", method = RequestMethod.POST)
public @ResponseBody String assignedParticularTable(@RequestBody HashMap<String, String> jsonValues, HttpSession session) {
    System.out.println("operation: "+jsonValues); 
    System.out.println("operation1: "+jsonValues.get("paymentType"));           
    return "";
}

答案 1 :(得分:-1)

尝试传递数组中的数据值,您可以根据编号动态构建数组。您需要传递的变量和值。