简体   繁体   English

通过 ajax 将数组发布到 Spring 启动控制器

[英]Post array by ajax to Spring boot controller

my ajax function called as我的 ajax 函数称为

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

Where variable "drivers" and "constIds" are array object, output by browser console like其中变量“drivers”和“constIds”是数组对象,由浏览器控制台输出,如

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

My controller:我的控制器:

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

}

But the output is但输出是

Your Data =>["0"
Your Data =>"31"
Your Data =>"30"]

So how can I get rid of those brackets, then I can parse the string to Integer.那么我怎样才能摆脱那些括号,然后我可以将字符串解析为整数。

The brackets are appearing because you are calling JSON.stringify on the arrays, which is unnecessary.出现括号是因为您在数组上调用JSON.stringify ,这是不必要的。 Try this:尝试这个:

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

I faced a list of errors, attaching below the final working code..我遇到了一系列错误,附在最终工作代码下方。

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

and at Controller side :并在控制器端:

@ResponseBody
@RequestMapping(path = "/item/refresh", method = RequestMethod.POST)
public String BookingItemList(@RequestBody Long[] itemIDs) //RequestBody instead of regular parameter

References : https://stackoverflow.com/a/11549679/557968 https://stackoverflow.com/a/32970230/557968参考资料: https : //stackoverflow.com/a/11549679/557968 https://stackoverflow.com/a/32970230/557968

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM