admin-ajax returning 400 error when request is made with Fetch API

To make a working HTTP request with fetch API you have to do something like this.

const form = new FormData();
form.append('action', 'make_appointment');
form.append('post_title', 'hola como estas');
const params = new URLSearchParams(form);

fetch(ajaxSettings.ajaxurl, {
      method: 'POST',
      credentials: 'same-origin',
      headers: {
       'Content-Type': 'application/x-www-form-urlencoded',
       'Cache-Control': 'no-cache',
      },
      body: params
    }).then(response => {
       return response.json();
    })
    .then(response => {
       // read data here
       console.log(response);
     }).catch(err => { 
       console.log(err);
     });

Leave a Comment

techhipbettruvabetnorabahisbahis forumutaraftarium24edusedueduseduseduedueduedueduedu