【问题标题】:create a List of Map javascript?创建地图javascript列表?
【发布时间】:2016-03-31 15:40:32
【问题描述】:

大家好,请问如何获取我在 javascript 中创建的对象数组并发送到 spring 控制器。

$(table).find('tr').each(function(index) {
                        $this = $(this)
                        var value = $this.find("span.value").html();
                        var quantity = $this.find("input.quantity").val();
                        var lots = $this.find(".lots").val(), produits = $this
                                .find(".produits").val(), fournisseurs = $this
                                .find(".fournisseurs").val(), ref = $this
                                .find(".ref").html(), unite = $this
                                .find(".unite").val(), prix = $this
                                .find(".prix").html(), qte = $this
                                .find(".qte").val(), total = $this
                                .find(".total").text();
                        var ligne = new Object();
                        ligne.lot = lots;
                        ligne.produit = produits;
                        ligne.fournissuer = fournisseurs;
                        ligne.unite = unite;
                        ligne.prix = prix;
                        ligne.total = total;
                        ligne.ref = ref;
                        ligne.qte = qte;
                        tableau.push(ligne)

                    });

这是我的 Ajax 请求

 $.ajax({
        traditional : true,
        async : false,
        url : '/save/',
        contentType : "application/json",
        type : "POST",
        data : JSON.stringify(tableau),
        success : function(data) {
            console.log("okk ");
        },
        error : function(jqXHR, textStatus, errorThrown) {     
            console.log('erreur');
        }

还有 Spring 控制器。

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@RequestBody ArrayList<ChiffrageLigne> tableau) {
    log.info("liste des donnees = "+tableau.size());
    for (ChiffrageLigne chiffrageLigne : tableau) {
        System.out.println("ref fournisseur"+chiffrageLigne.getRefFour()+"prix ="+chiffrageLigne.getPrix());
    }
    return "ok";
}

那么我如何在 sping mvc 控制器中获取这个数组。谢谢

【问题讨论】:

  • 你能不能尝试通过JSON.stringify(ligne);将此对象作为JSON发送到spring mvc。
  • 我这样做了,但没有任何改变它给了我错误“NetworkError: 400 Mauvaise Requête - localhost:8080/project/requst

标签: javascript list spring-mvc controller


【解决方案1】:

您可以在不使用 ModelAttribute 的情况下从 Ajax 获取 List&lt;Object&gt; 而不是 List&lt;ChiffrageLigne&gt;

 $.ajax({
        traditional : true,
        async : false,
        url : '/save/',
        contentType : "application/json",
        type : "POST",
        dataType : "json",
        data : {tableau:JSON.stringify(tableau)},
        success : function(data) {
            console.log("okk ");
        },
        error : function(jqXHR, textStatus, errorThrown) {     
            console.log('erreur');
        }

控制器代码

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@RequestParam(value="tableau",required=true)  List<Object> tableauList) {
    //write code here converting List<object> to ArrayList<ChiffrageLigne>
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2020-11-05
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多