前台ajax傳數組,后台java接收


 

 

后端

//添加
    @RequestMapping(value = "checkChoise")
    @ResponseBody
    ResultJson checkChoise(@RequestBody List<String> arr) {
        ResultJson resultJson = new ResultJson();
        System.out.println("str:"+arr);
        resultJson.setStatus(0);
        resultJson.setMessage("添加成功!");
        resultJson.setData(null);
        return resultJson;
    }

 

 

js

//某個角色函數
function comfirm(){

     //獲取checkbox[name='check']的值
     var arr = new Array();
     $("input:checkbox[name='check']:checked").each(function(i){
           arr[i] = $(this).val();
     });   
    $.ajax({
        type: "post",//注意不能用get
        dataType: 'json',
        url: "/checkChoise",
        contentType: 'application/json',//這個必須是這個格式
        data: JSON.stringify(arr),//前台要封裝成json格式
        success: function (result) {
            if (result.status == 0) {
                
            }
            if (result.message != null) {
                layer.msg(result.message)
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            layer.msg('{"status":"' + XMLHttpRequest.status + '","readyState":"' + XMLHttpRequest.readyState + '","textStatus":"' + textStatus + '","errorThrown":"' + errorThrown + '"}')
        }
    })
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM