简体   繁体   English

将数组传递给 $.ajax() 中的 ajax 请求到控制器 Spring mvc

[英]Pass array to ajax request in $.ajax() to controller Spring mvc

I want to pass an array of strings as well as a string variable to the controller but it says parameter not present.我想将一个字符串数组和一个字符串变量传递给控制器​​,但它说参数不存在。

here is my javascript这是我的 javascript

function updateIssueHandler() {
var keyArray = [];
bootstrap_alert_hide('#form_errors');// cleans previous errors
$('input:checkbox[name=key_checkbox]:checked').each(function() {
    keyArray.push($(this).val())
});
if (keyArray.length == 0) {
    var errorMsg = document
            .getElementById("js.i18nMessage.emptyUpdateTickets.id").value;
    bootstrap_alert('#form_errors', errorMsg)
}
var labels = document.getElementById('labelsTextBox').value;
var b = {
    "lab" : labels,
    "keys": keyArray
};
//console.log(addl);
console.log(keyArray);
$.ajax({
    url : '/jirabatchapp/JqlUpdate/',
    data : b,
    type : 'POST',
    success : function(data) {
        window.alert("done");
    },
    error : function(e) {
        alert(JSON.stringify(e));
    }
});
}

Controller:控制器:

@Controller
public class UpdateLabelController {

@RequestMapping(value = "/JqlUpdate", method = RequestMethod.POST)
public String updateIssue(@RequestParam(value = "lab") String a,@RequestParam(value = "keys") String k ) {

    System.out.println(a);
    System.out.println(k);
    System.out.println("finally!");
    return "success";
}
}

I get an error saying HTTP request error 400 Required string parameter lab not present.我收到一条错误消息,说 HTTP 请求错误 400 所需的字符串参数实验室不存在。

What am i doing wrong?我究竟做错了什么? if I only pass labs, then it works, but if i want to pass both parameters it is throwing me this error.如果我只通过实验室,那么它可以工作,但如果我想传递这两个参数,它就会抛出这个错误。

change改变

var b = {
    "lab" : labels
    "keys": keyArray
};

to

var b = {
    "lab" : labels,
    "keys": keyArray
};

You have to pass a string as parameters你必须传递一个字符串作为参数

Just do this : data : JSON.stringify(b)只需这样做: data : JSON.stringify(b)

EDIT :编辑 :

Try : data: $.param(b)尝试: data: $.param(b)

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

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