sunmingtao / sample-code

3 stars 4 forks source link

Ajax request is blocked due to MIME type (“application/json”) mismatch (X-Content-Type-Options: nosniff). #229

Closed sunmingtao closed 3 years ago

sunmingtao commented 3 years ago

Send an ajax request to following route:


@GetMapping(value = "/menu")
public String getDeliverySystemMenu() {
    return "...";
}
sunmingtao commented 3 years ago

Turns out by default the content type is "application/json".

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer
            .ignoreAcceptHeader(true)
            .defaultContentType(MediaType.APPLICATION_JSON, MediaType.ALL)
            .mediaType("json", MediaType.APPLICATION_JSON);
}

The fix is overwrite the content type:

@GetMapping(value = "/menu",  produces = "application/javascript; charset=utf-8")
public String getDeliverySystemMenu() {
    return "...";
}