【问题标题】:Pass array to ajax request in $.ajax() to controller Spring mvc将数组传递给 $.ajax() 中的 ajax 请求到控制器 Spring mvc
【发布时间】:2017-02-12 11:31:21
【问题描述】:

我想将一个字符串数组和一个字符串变量传递给控制器​​,但它说参数不存在。

这是我的 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
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";
}
}

我收到一条错误消息,提示 HTTP 请求错误 400 所需的字符串参数实验室不存在。

我做错了什么?如果我只通过实验室,那么它可以工作,但如果我想同时传递这两个参数,它会抛出这个错误。

【问题讨论】:

    标签: javascript jquery ajax spring model-view-controller


    【解决方案1】:

    你必须传递一个字符串作为参数

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

    编辑:

    试试看:data: $.param(b)

    【讨论】:

    • 仍然抛出 Http status 400 required string "lab" not present
    • 当我这样做时,它会抛出 Http status 400 required parameter string 'keys' not present
    • 但是这次你有实验室吗?奇怪 ^^ 你能在调试器工具的网络选项卡中显示查询字符串参数吗?
    • 请求 URL:localhost:8080/jirabatchapp/JqlUpdate 请求方法:POST 状态码:400 错误请求远程地址:[::1]:8080 响应头查看源缓存控制:no-cache, no-store, max-age=0, must-revalidate Connection:close Content-Language:en Content-Length:1105 Content-Type:text/html;charset=utf-8 Date:Tue, 04 Oct 2016 11:00:19 GMT 过期时间: 0 Pragma:no-cache 服务器:Apache-Coyote/1.1 X-Content-Type-Options:nosniff X-Frame-Options:DENY X-XSS-Protection:1; mode=block Request Headers 查看源 Accept:*/*
    • Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8 Connection:keep-alive Content-Length:55 Content-Type:application/x-www-form- urlencoded; charset=UTF-8 Cookie:JSESSIONID=96CA5D33BDFB6D37C4FF3361A3401E85 Host:localhost:8080 Origin:localhost:8080Referer:localhost:8080/jirabatchapp/JqlSearch User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 X-Requested-With:XMLHttpRequest Form Data view source view URLcoded lab:lol keys[]:ACADPLY-4432 keys[]:ACADPLY-4388
    【解决方案2】:

    改变

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

    到

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

    【讨论】:

    • 对不起,我已经这样做了。在编辑中更改了它。仍然抛出同样的错误
    • console.log 中的 keyArray 值是什么?
    • 看起来您在键中传递字符串数组,并且您只在控制器端检索字符串。如果你想要 String 然后传递 keyArray.toString(); b 变量中的值
    • ["ACADPLY-4432", "ACADPLY-4388"] 0 : "ACADPLY-4432" 1 : "ACADPLY-4388" 长度: 2
    • @RequestParam(value = "keys[]") String[] k
    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 2012-02-12
    相关资源
    最近更新 更多