1

I just can not. For 4 days I have been struggling to send data ajax using a different method than GET and I can not advise. I create a project in Spring Boot. I have created a mapped address controller

@PutMapping(value = "/changeEmail", consumes = MediaType.APPLICATION_JSON_VALUE)
public boolean changeEmail(
    @RequestBody ChangeEmailDTO changeEmailDTO
) {
    System.out.println("email: " + changeEmailDTO.getEmail());
    return true;
}

This controller is supposed to accept the email address sent by ajaxa

function changeEmail() {
    console.log("Event");
    $.ajax({
        type: 'PUT',
        url: '/changeEmail',
        data: {
            email: JSON.strgify($('#email').val())
        },
        success: function (result) {
            console.log('function');
        }
    });
}

However, the only effect is that in the console crashes me

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

The DTO has only one field

public class ChangeEmailDTO {
    @IsValidEmail
    @ExistsEmail(ifExistsReturn = false)
    @Getter @Setter private String email;
}

After trying to send data via ajax, it sends me to the error controller

@Controller
public class PageNotFoundController implements ErrorController{
    @RequestMapping("/error")
    public ModelAndView showPageError() {
        return new ModelAndView("redirect:/signIn");
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

Screen with browser tool what to send and receive: https://zapodaj.net/f1b8ed0b2a16b.png.html As I mentioned, the controller can correctly pick up a GET query from ajax only. Every time it flips on the error controller. Additionally, they say that if I wanted to send data directly from the form, then there is no problem with any query. What this ajax divert, I do not know.

In addition, when the csrf() is disabled in Spring Security receives such logs

    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

The most important mistakes are

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
  • Possible duplicate of [Send JSON data with jQuery](https://stackoverflow.com/questions/6587221/send-json-data-with-jquery) – AgentBawls Jul 26 '17 at 19:48

1 Answers1

0

Your error message tells you your issue: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

Add contentType: 'application/json; charset=utf-8', to your ajax call. You'll also want to call JSON.stringify on the entire JSON string. Check out the w3schools page on stringify for a good example, also copied/pasted below.

var obj = { "name":"John", "age":30, "city":"New York"};
var myJSON = JSON.stringify(obj);
AgentBawls
  • 109
  • 12
  • is email in quotes, as required by json? Is your value in quotes? Your JSON needs to be exact. – AgentBawls Jul 26 '17 at 19:40
  • How about quotes? In this place 'email: JSON.stringify($('#email').val())' I send data directly from the text box. –  Jul 26 '17 at 19:44
  • Your entire json string needs to be your argument to `JSON.stringify`, not just the value. `{"email":"$('#email').val()"}` should be what's getting put into json.stringify, not just `$('#email').val()` – AgentBawls Jul 26 '17 at 19:47
  • I will not write your code for you. Take a look at https://stackoverflow.com/questions/6587221/send-json-data-with-jquery the data you send in your call needs to be JSON. This will not result in valid json inside the '`data` field: `data: { email: data },` – AgentBawls Jul 26 '17 at 20:00
  • I love you. Everything works. Thanks. Last question I have. Why do I have to disable scrf () in Spring Security? –  Jul 26 '17 at 20:10
  • Otherwise, you'd need to include a csrf token with every call. If you're not dealing with sensitive data, it isn't worth it. Take a look at Spring's explanation of CSRF: https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html – AgentBawls Jul 26 '17 at 20:16
  • So you're advising me to turn off csrf () and use the token when needed. –  Jul 26 '17 at 20:40