通过ajax将数组发布到Spring引导控制器

时间:2017-05-16 09:06:20

标签: jquery ajax spring

我的ajax函数叫做

// webpack.config.js
entry: {
  main: [
    path.resolve(__dirname, 'client_patch.js'),
    'index.js'
  ]
}

变量"驱动程序"和#34; constIds"是数组对象,由浏览器控制台输出,如

    $.ajax({
            url:'${pageContext.request.contextPath}'+'/admin/sendMsg',
            method:'POST',
            traditional: true,
            data:{
                driverList: JSON.stringify(drivers),
                constList: JSON.stringify(constIds),
                content: content
            },
            success: function(data) {
                        if (data == "FAIL") {
                            alert("File not found!");
                        } 
                    },
                    error: function(request, status, error) {
                        alert("The request failed: " + request.responseText);
                    }
        });

我的控制器:

["0", "31", "30"]
0: "0"
1: "31"
2: "30"
length: 3__proto__: Array(0)

但输出是

@ResponseBody
    @RequestMapping(value = "/sendMsg", method = RequestMethod.POST)
    public void sendMsg(HttpSession session,
            @RequestParam(value = "driverList")String[] driverList,
            @RequestParam(value = "constList")String[] constList, @RequestParam(value = "content")String content)
{
    for (String data : driverList) {
         System.out.println("Your Data =>" + data);
    }
    System.out.println(constList);

}

那么如何摆脱这些括号,然后我可以将字符串解析为Integer。

2 个答案:

答案 0 :(得分:0)

括号出现是因为您在数组上调用function getTimeRemaining(endtime) { var t = Date.parse(endtime) - Date.parse(new Date()); var seconds = Math.floor((t / 1000) % 60); var minutes = Math.floor((t / 1000 / 60) % 60); var hours = Math.floor((t / (1000 * 60 * 60)) % 24); var days = Math.floor(t / (1000 * 60 * 60 * 24)); return { 'total': t, 'days': days, 'hours': hours, 'minutes': minutes, 'seconds': seconds }; } function initializeClock(id, endtime) { var clock = document.getElementById(id); var daysSpan = clock.querySelector('.days'); var hoursSpan = clock.querySelector('.hours'); var minutesSpan = clock.querySelector('.minutes'); var secondsSpan = clock.querySelector('.seconds'); function updateClock() { var t = getTimeRemaining(endtime); daysSpan.innerHTML = t.days; hoursSpan.innerHTML = ('0' + t.hours).slice(-2); minutesSpan.innerHTML = ('0' + t.minutes).slice(-2); secondsSpan.innerHTML = ('0' + t.seconds).slice(-2); if (t.total <= 0) { clearInterval(timeinterval); swal("Oops", "Your time is up! Test will be submitted automatically.", "error"); setTimeout(function (){ $(document).ready(function(){$("#submitLinguistics").click();}); clearInterval(myInterval); window.onbeforeunload = null; },3000); } } updateClock(); var timeinterval = setInterval(updateClock, 1000); } sessionStorage.setItem('deadline', new Date(Date.parse(new Date() ) + (40* 60 * 1000))); initializeClock('clockdiv', sessionStorage.getItem('deadline')); ,这是不必要的。试试这个:

JSON.stringify

答案 1 :(得分:0)

我遇到了一系列错误,这些错误附加在最终工作代码的下面。

df1 <- iris
df1$clr <- ave(df1$Sepal.Length, df1$Species, FUN = function(x){
  runif(length(x), 0, max(x))
})

以及在控制器端:

$.ajax({
    type: "POST",
    headers: { //Required to avoid 415 error
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    url: "/item/refresh",
    data: JSON.stringify(itemIDs), //itemIDs = ["5", "3", "8"]
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){
        alert(data);
    },
    failure: function(errMsg) {
        alert(errMsg);
    }
});

参考文献: https://stackoverflow.com/a/11549679/557968 https://stackoverflow.com/a/32970230/557968