ReactJS 从 Promise 获取结果
ReactJS getting results from a Promise
在我的 sagas 中,我将用户凭据发送到后端
const request = client.post('api/JwtAuth/GenerateToken', {UserName: action.username, Password: action.password})
console.log(request)
客户看起来像这样
从“axios”导入 axios
const client = axios.create({
baseURL: 'https://localhost:5001/',
timeout: 600000000000,
headers: {
ContentType: 'application/json'
}
})
export default client
const request = client.post('api/JwtAuth/GenerateToken', {UserName:
action.username, Password: action.password})
request.then(response => {
// the response body can be accessed through the "data" property
console.log(response.data)
})
console.log(request)
更新:
你是在 saga 里面做这个,对吧?
如果是这样,获取请求响应数据的最佳方式是使用 call
效果。从 redux-saga/effects
导入它,得到这样的响应:
const response = yield call(
client.post,
'api/JwtAuth/GenerateToken',
{ UserName: action.username, Password: action.password }
)
console.log(response.data)
你的问题是什么?如果问题是为什么您会在控制台上看到 Promise?那是因为你正在处理承诺,要么使用 .then
client.post().then(res => console.log(res.data)).catch(err => console.log(err))
或异步等待语法
await client.post()
在我的 sagas 中,我将用户凭据发送到后端
const request = client.post('api/JwtAuth/GenerateToken', {UserName: action.username, Password: action.password})
console.log(request)
客户看起来像这样
从“axios”导入 axios
const client = axios.create({
baseURL: 'https://localhost:5001/',
timeout: 600000000000,
headers: {
ContentType: 'application/json'
}
})
export default client
const request = client.post('api/JwtAuth/GenerateToken', {UserName:
action.username, Password: action.password})
request.then(response => {
// the response body can be accessed through the "data" property
console.log(response.data)
})
console.log(request)
更新:
你是在 saga 里面做这个,对吧?
如果是这样,获取请求响应数据的最佳方式是使用 call
效果。从 redux-saga/effects
导入它,得到这样的响应:
const response = yield call(
client.post,
'api/JwtAuth/GenerateToken',
{ UserName: action.username, Password: action.password }
)
console.log(response.data)
你的问题是什么?如果问题是为什么您会在控制台上看到 Promise?那是因为你正在处理承诺,要么使用 .then
client.post().then(res => console.log(res.data)).catch(err => console.log(err))
或异步等待语法
await client.post()