简体   繁体   English

尝试通过ajax的xmlhttprequest向控制器发布多个变量

[英]Trying to post more than one variable via xmlhttprequest of ajax to the controller

In my java application. 在我的Java应用程序中。 I am trying to post more two javascript variable to via a xmlhttp post request of ajax but it is returning error 400. Ordinarily, if I comment out one of the post variables, then it posts successfully but on adding it, throws error. 我正在尝试通过ajax的xmlhttp发布请求发布更多两个javascript变量,但它返回错误400。通常,如果我注释掉其中一个发布变量,则它成功发布,但是在添加它时抛出错误。 This is my attempt: 这是我的尝试:

function sendChat() {
        var message = document.getElementById('new-input').value.trim();
        var noteVal = "mp3",
                xhr = new XMLHttpRequest();

        ....

        alert(noteVal);
        xhr.send(encodeURI('message=' + message));
        xhr.send(encodeURI('noteVal=' + noteVal)); //if I comment this out then everything is fine
    }

this is the spring controller to receive the post arguments 这是用于接收post参数的spring控制器

@ResponseBody
    @RequestMapping(value = "/post-data", method = RequestMethod.POST)
    public ModelAndView postData(HttpServletRequest request,
                           HttpServletResponse response,
                           @RequestParam String message,
                           @RequestParam String noteVal,

Please how can I post more than one variables to the controller method via XMLHttpRequest 请如何通过XMLHttpRequest将多个变量发布到控制器方法

You can only send once, so you send both values 您只能发送一次,所以您同时发送两个值

xhr.send('message=' + encodeURI(message) + '&noteVal=' + encodeURI(noteVal));

or sending formData 或发送formData

var data = new FormData();
data.append('message', message);
data.append('noteVal', noteVal);

xhr.send(data);

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

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