1

Working with DataTables. Getting a 403 Forbidden error. Please Help

Working based on https://datatables.net/examples/data_sources/server_side.html

$(document).ready(function() {
     $('#example').DataTable( {
    "processing": true,
    "serverSide": true,
     "ajax": "response1.php",
     "columns": [
            { "data": "empid" },
            { "data": "empname" },
            { "data": "salary" }]
   } );
 } );

When calling response1.php - I get the required output in correct format, but when i include the call in ready from another page . I get DataTables warning: table id=example - Ajax error. For more information about this error, please see http://datatables.net/tn/7. On debugging i get 403 from the browser. Please help. response1.php in in the same directory

Reshma CB
  • 61
  • 2
  • 8

1 Answers1

0

When you use spring boot with spring security and if you are accessing your API's(POST, PUT, DELETE) from Postman or something, they wont be accessible and error is related to authorization like forbidden 403. One solution is disabling csrf but this makes application less secure, best thing to do is send csrf token. To know if this is your problem you can create this class in order to disable the csrf:

@Configuration
@EnableWebSecurity
public class AppWebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .anyRequest().permitAll();
    }
}

Then you can learn to send de csrf in the ajax request reading this other question: Adding CSRFToken to Ajax request

You can get more information about this problem at https://newbedev.com/403-forbidden-when-i-try-to-post-to-my-spring-api

abanet
  • 1,327
  • 17
  • 22