I use Fetch Api with WordPress admin-ajax.php.
let data = {
action: 'my_action',
}
fetch( ajaxurl, {
method: 'POST',
credentials: 'same-origin',
body: new URLSearchParams(data)
});
let data = New FormData();
data.append('action', 'my-action');
fetch( ajaxurl, {
method: 'POST',
credentials: 'same-origin',
body: data
});
let data = {
action: 'my_action',
}
fetch( ajaxurl, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
I'm curious why it got 400 error? anything I miss?
change your fetch login as below and try. Since you are specifying 'Content-Type' : 'json' , no need to stringify the json.
fetch( ajaxurl, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
body: data
});