反应ajax请求

Can somebody help me with ajax request to server? I'm trying to send data to server

static language(lang) {

    const request = new Request("/change-language", {
        method: 'POST',
        body: JSON.stringify({lang: lang})
    });

    return fetch(request).then(response => {
        return response.json();
    }).catch(error => {
        return error;
    });
}

Function with ajax request

But this function doesn't work properly

Here jquery ajax request and he is works like expected

$.ajax({
  type: 'POST',
  url: '/change-language',
  data: {lang: 'esp' },
success: function(data){
  console.log(data)
 }
});

any ways to make it work?

Thanks a lot!

Your jQuery version is encoding the data as x-www-form-urlencoded but your fetch version is encoding the data as JSON.

You need to either continue to encode the data using the x-www-form-urlencoded format, or change your server side code to expect JSON.