Ajax 将请求放入Spring引导控制器
我正试图用ajax向我的spring boot controller发送一个json字符串,但我不断收到错误net::ERR_太多_重定向,url后面有一堆$csrfError。我看了很多其他的网站和问题,但我还是无法让它发挥作用。如何正确设置ajax请求,以便我的控制器接受数据 我已经尝试过将方法类型更改为post,并将RequestParams更改为ResponseBy,但我还是回到了RequestParamsAjax 将请求放入Spring引导控制器,ajax,spring-boot,Ajax,Spring Boot,我正试图用ajax向我的spring boot controller发送一个json字符串,但我不断收到错误net::ERR_太多_重定向,url后面有一堆$csrfError。我看了很多其他的网站和问题,但我还是无法让它发挥作用。如何正确设置ajax请求,以便我的控制器接受数据 我已经尝试过将方法类型更改为post,并将RequestParams更改为ResponseBy,但我还是回到了RequestParams my ajax function function deleteEvent()
my ajax function
function deleteEvent() {
var startTime = document.getElementById('newStartTime').value;
var endTime = document.getElementById('newEndTime').value;
var type = document.getElementById('newTimeOffType').value;
var search = {startTime: startTime, endTime: endTime, type: type};
if(confirm("Are you sure you want to delete this event?")){
$.ajax({
method: 'PUT',
contentType: 'application/json',
url: '/vacation/deleteEvents',
dataType: 'json',
data: JSON.stringify(search),
success: function () {
console.log('success');
},
error: function (e) {
console.log(e);
}
})
}
else {
alert('you said no')
}
我的弹簧控制器
@RequestMapping(value = "/vacation/deleteEvents", method=RequestMethod.PUT)
@ResponseBody
public String deleteCalendarEvents(@RequestParam String startTime, @RequestParam String endTime, @RequestParam String type, HttpSession session ){
User user = (User) session.getAttribute("currentUser");
System.out.println("startTime: " + startTime);
System.out.println("endTime: " + endTime);
System.out.println("type: " + type);
return null;
}
我希望它打印出开始时间、结束时间,并在我的控制台中键入,但我一直收到此错误。您是否使用
邮递员尝试过?因为您可以知道后端是否失败。如果您不希望返回任何内容,为什么不将其作废?返回类型可能会导致问题。我不知道邮递员是什么,所以我没有尝试过,返回最初用于重定向回主页@JonathanJohxi尝试运行ajax请求。我收到错误net::ERR_TOO_许多重定向,url后有一堆$csrfError@ReflectionDemon当您将返回类型设置为void时,是否删除了@ResponseBody?