作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试使用 ajax 发送请求,但状态为 400 错误请求。我应该发送什么样的数据以及如何在 Controller 中获取数据?我确定请求没问题,只是参数出错了
jsp
<script type="text/javascript">
var SubmitRequest = function(){
$.ajax({
url : "submit.htm",
data: document.getElementById('inputUrl'),
type: "POST",
dataType: "text",
contentType: false,
processData: false,
success :
function(response) {
$('#response').html(response);
}
});
}
</script>
Controller
@RequestMapping(value = "/submit", method = RequestMethod.POST)
public @ResponseBody
String Submit(@RequestParam String request) {
APIConnection connect = new APIConnection();
String resp = "";
try {
resp = "<textarea rows='10'cols='100'>" + connect.doConnection(request) + "</textarea>";
} catch (Exception e) {
// TODO Auto-generated catch block
resp = "<textarea rows='10'cols='100'>" + "Request failed, please try again." + "</textarea>";
}
return resp;
}
最佳答案
要发送 Ajax 发布请求,您可以使用:
$.ajax({
type: "POST",
url: "submit.htm",
data: { name: "John", location: "Boston" } // parameters
})
在 Spring MVC 中 Controller :
@RequestMapping(value = "/submit.htm", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
String Submit(@RequestParam("name") String name,@RequestParam("location") String location) {
// your logic here
return resp;
}
关于javascript - 如何在 Spring Framework 中发送和接收带参数的 ajax 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156748/
我是一名优秀的程序员,十分优秀!