Using axios to make a post request - node js

Viewed 530

I'm new to node.js and i'm trying to do a post request from server side using axios But i get no response data from the request.

If i do a post request from the client side using ajax it works fine and i was just wondering am i doing something wrong?

My server side post request:

var postData ={
  a: 'getMessages',
  max: 50,
  id: 5039
};

let postConfig = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  }
};

axios.post('https://url.com/ajax.php', postData, postConfig).then((response) => {
  console.log('response:', response.data);
}).catch((error) => {
  console.log('error:', error);
});

The way i was doing post request from client side:

This worked fine but when i try to do a post request from server side there is no response data.

$.post('https://url.com/ajax.php', {
  a: 'getMessages',
  max: 50,
  id: 5039
}, function(response) {
  let data = JSON.parse(response);

  console.log('data:', data);
});
1 Answers

Client-side, it looks like you're using jQuery $.post(), which defaults to posting JSON when sending an object like this.

I suspect that the issue is that your content type is sent to be a form. If you simply get rid of postConfig entirely, it should send it as application/json, and should properly format the request body for what you want.

Related