在我的java应用程序中。我试图通过ajax的xmlhttp发布请求发布更多两个javascript变量,但它返回错误400.通常,如果我注释掉其中一个帖子变量,那么它发布成功但在添加它时,抛出错误。这是我的尝试:
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
}
这是接收post参数的spring控制器
@ResponseBody
@RequestMapping(value = "/post-data", method = RequestMethod.POST)
public ModelAndView postData(HttpServletRequest request,
HttpServletResponse response,
@RequestParam String message,
@RequestParam String noteVal,
请问如何通过XMLHttpRequest
向控制器方法发布多个变量答案 0 :(得分:1)
您只能发送一次,因此您发送两个值
xhr.send('message=' + encodeURI(message) + '¬eVal=' + encodeURI(noteVal));
或发送formData
var data = new FormData();
data.append('message', message);
data.append('noteVal', noteVal);
xhr.send(data);