使用 Axios 时未定义的结果 POST
Undefined result when using Axios POST
我尝试在 axios 中使用 Post 方法发送一些数据,但我的代码的结果只有 undefined。
这是我使用 axios post http 请求的代码:
const Axios = use('axios');
const Env = use('Env');
const querystring = require('querystring');
class getTrackingData({ response }) {
const tracking = await Axios.post(Env.get('APP_ENDPOINT') + '/waybill',
{
data: querystring.stringify({
waybill : 'SOCAG00183235715', courier : 'jne'
})
},
{
headers: {
'key':Env.get('APP_KEY'),
'content-type': "application/x-www-form-urlencoded"
}
}).then(function(response) {
console.log(response.data);
//return response.data;
});
return tracking;
}
}
这段代码有什么问题?
试试这个
'content-type': "application/application.json"
详情example
更新
请将此添加到 header
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
我不知道,但我认为这是因为发送的数据格式不可读,所以我试图改变发送数据的格式:
const waybill = 'SOCAG00183235715'
const courier = 'jne'
const tracking = await Axios.post(Env.get('APP_ENDPOINT') + '/waybill',
//{
//data: querystring.stringify({
// waybill : 'SOCAG00183235715', courier : 'jne'
//})
//},
//change code above to bellow:
'waybill='+waybill+'&courier='+courier,
{
headers: {
'key':Env.get('APP_KEY'),
'content-type': "application/x-www-form-urlencoded"
}
}).then(function(response) {
console.log(response.data);
//return response.data;
});
return tracking;
这就是我的工作!
我尝试在 axios 中使用 Post 方法发送一些数据,但我的代码的结果只有 undefined。 这是我使用 axios post http 请求的代码:
const Axios = use('axios');
const Env = use('Env');
const querystring = require('querystring');
class getTrackingData({ response }) {
const tracking = await Axios.post(Env.get('APP_ENDPOINT') + '/waybill',
{
data: querystring.stringify({
waybill : 'SOCAG00183235715', courier : 'jne'
})
},
{
headers: {
'key':Env.get('APP_KEY'),
'content-type': "application/x-www-form-urlencoded"
}
}).then(function(response) {
console.log(response.data);
//return response.data;
});
return tracking;
}
}
这段代码有什么问题?
试试这个
'content-type': "application/application.json"
详情example
更新
请将此添加到 header
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
我不知道,但我认为这是因为发送的数据格式不可读,所以我试图改变发送数据的格式:
const waybill = 'SOCAG00183235715'
const courier = 'jne'
const tracking = await Axios.post(Env.get('APP_ENDPOINT') + '/waybill',
//{
//data: querystring.stringify({
// waybill : 'SOCAG00183235715', courier : 'jne'
//})
//},
//change code above to bellow:
'waybill='+waybill+'&courier='+courier,
{
headers: {
'key':Env.get('APP_KEY'),
'content-type': "application/x-www-form-urlencoded"
}
}).then(function(response) {
console.log(response.data);
//return response.data;
});
return tracking;
这就是我的工作!