简体   繁体   中英

Using axios to make a post request - node js

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);
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM