Андрей Гузюк Андрей Гузюк - 4 years ago 209
React JSX Question

React ajax request

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!

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download