我使用了请求并用承诺包装它,但我想用axios编写更清晰的代码,不知何故我得到了内部serval错误(请求失败,状态代码为401),我无法访问后端代码I不知道发生了什么事。
//worked
response = yield new Promise(resolve => {
resolve(request.post(api, {form: {id: 1, user: 1}}))
})
//doesn't work
response = yield axios.post(api, {id: 1, user: 1})
答案 0 :(得分:1)
啊,你发送的是application / x-www-form-urlencoded请求!事实证明,使用axios并不是那么容易。在自述文件中:
https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format
那里描述了许多技术。这是在节点的服务器端工作的那个:
var querystring = require('querystring')
axios.post('/foo', querystring.stringify({id: user: 1})
在这种情况下,请求会为您提供更清晰的代码!