我不能。 4天我一直在努力使用与GET不同的方法发送数据ajax,我无法建议。我在Spring Boot中创建了一个项目。我创建了一个映射的地址控制器
@PutMapping(value = "/changeEmail", consumes = MediaType.APPLICATION_JSON_VALUE)
public boolean changeEmail(
@RequestBody ChangeEmailDTO changeEmailDTO
) {
System.out.println("email: " + changeEmailDTO.getEmail());
return true;
}
该控制器应该接受ajaxa发送的电子邮件地址
function changeEmail() {
console.log("Event");
$.ajax({
type: 'PUT',
url: '/changeEmail',
data: {
email: JSON.strgify($('#email').val())
},
success: function (result) {
console.log('function');
}
});
}
然而,唯一的影响是在控制台崩溃了我
PUT http://localhost:8080/signIn net::ERR_TOO_MANY_REDIRECTS
send @ jquery-3.2.1.min.js:4
ajax @ jquery-3.2.1.min.js:4
changeEmail @ settings.js:58
submitHandler @ settings.js:52
d @ jquery.validate.min.js:4
(anonymous) @ jquery.validate.min.js:4
dispatch @ jquery-3.2.1.min.js:3
q.handle @ jquery-3.2.1.min.js:3
DTO只有一个字段
public class ChangeEmailDTO {
@IsValidEmail
@ExistsEmail(ifExistsReturn = false)
@Getter @Setter private String email;
}
尝试通过ajax发送数据后,它将我发送到错误控制器
@Controller
public class PageNotFoundController implements ErrorController{
@RequestMapping("/error")
public ModelAndView showPageError() {
return new ModelAndView("redirect:/signIn");
}
@Override
public String getErrorPath() {
return "/error";
}
}
使用浏览器工具屏幕要发送和接收的内容:https://zapodaj.net/f1b8ed0b2a16b.png.html 正如我所提到的,控制器只能从ajax正确地获取GET查询。每次它在错误控制器上翻转。 此外,他们说,如果我想直接从表单发送数据,那么任何查询都没有问题。 这个阿贾克斯转移了什么,我不知道。
此外,当Spring Security中禁用csrf()时会收到此类日志
2017-07-26 21:07:15.465 DEBUG 3760 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-07-26 21:07:15.465 DEBUG 3760 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-07-26 21:07:15.499 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/changeEmail]
2017-07-26 21:07:15.501 DEBUG 3760 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /changeEmail
2017-07-26 21:07:15.505 DEBUG 3760 --- [nio-8080-exec-7] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.506 DEBUG 3760 --- [nio-8080-exec-7] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.506 DEBUG 3760 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.508 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-07-26 21:07:15.508 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-07-26 21:07:15.509 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/error]
2017-07-26 21:07:15.510 DEBUG 3760 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-07-26 21:07:15.511 DEBUG 3760 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView com.movie.database.app.controller.error.PageNotFoundController.showPageError()]
2017-07-26 21:07:15.526 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.cors.DefaultCorsProcessor : Skip CORS processing: request is from same origin
here
2017-07-26 21:07:15.527 DEBUG 3760 --- [nio-8080-exec-7] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [*/*] based on Accept header types and producible media types [*/*])
2017-07-26 21:07:15.528 DEBUG 3760 --- [nio-8080-exec-7] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'redirect:/signIn'
2017-07-26 21:07:15.528 DEBUG 3760 --- [nio-8080-exec-7] o.s.w.s.v.ContentNegotiatingViewResolver : Returning redirect view [org.springframework.web.servlet.view.RedirectView: unnamed; URL [/signIn]]
2017-07-26 21:07:15.528 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.RedirectView: unnamed; URL [/signIn]] in DispatcherServlet with name 'dispatcherServlet'
2017-07-26 21:07:15.530 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-07-26 21:07:15.534 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/signIn]
2017-07-26 21:07:15.535 DEBUG 3760 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /signIn
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536 WARN 3760 --- [nio-8080-exec-9] o.s.web.servlet.PageNotFound : Request method 'PUT' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-07-26 21:07:15.537 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/error]
2017-07-26 21:07:15.538 DEBUG 3760 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-07-26 21:07:15.539 DEBUG 3760 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView com.movie.database.app.controller.error.PageNotFoundController.showPageError()]
2017-07-26 21:07:15.539 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.cors.DefaultCorsProcessor : Skip CORS processing: request is from same origin
最重要的错误是
2017-07-26 21:07:15.506 DEBUG 3760 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536 WARN 3760 --- [nio-8080-exec-9] o.s.web.servlet.PageNotFound : Request method 'PUT' not supported
答案 0 :(得分:0)
您的错误消息会告诉您问题:if(quitRequestedWhileWorkerBusy) this.Close();
将Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
添加到您的ajax调用中。您还需要在整个JSON字符串上调用contentType: 'application/json; charset=utf-8',
。查看w3schools page on stringify for a good example,同时复制/粘贴在下面。
JSON.stringify