gpt4 book ai didi

javascript - 如何在 Spring Framework 中发送和接收带参数的 ajax 请求?

转载 作者:IT老高 更新时间:2023-10-28 13:44:55 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com