Site icon Hip-Hop Website Design and Development

Can I use application/json content type in WordPress

I was using application/x-www-form-urlencoded and it was working all ok then I changed the content type to application/json and it is returning a 400 Bad Request error.

Is it that WordPress dosn’t support application/json or something else? And if WordPress dosn’t support it is there any way to enable it?

This is my code:

const search = document.querySelector('input.search')
const posts = document.querySelector('section.posts')

search.addEventListener('input', () => {

    const data = {
        action: 'data_fetch',
        keyword: `${search.value}`
    }

    const request = fetch('./wp-admin/admin-ajax.php', {
        headers: {
            'Content-Type': 'application/json'
        },
        method: 'POST',
        body: data
    })

    request.then(response => response.text())
    request.then(response => posts.innerHTML = response)
    request.catch(console.error)
    
})

Before I changed it to application/json the data was like this action=data_fetch&keyword=${search.value} and it was working.