【问题标题】:Pass Multiple data variables to the AJAX function in an easy way?以简单的方式将多个数据变量传递给 AJAX 函数?
【发布时间】:2016-02-10 13:17:14
【问题描述】:

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

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

Jsp页面:

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

控制器类:

@RequestMapping(value = "/assignedParticularTable.html", method = RequestMethod.POST)
public @ResponseBody String assignedParticularTable(@RequestParam HashMap<String, String> map, HttpSession session) {

    System.out.println("operation: " + map.get("operation"));

    return "";
}

【问题讨论】:

    标签: javascript jquery json ajax spring-mvc


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2015-08-19
        • 2019-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-10
        相关资源
        最近更新 更多