我有一个关于缩放API的问题。如何在axuos的reajJs中获取nodeJs API数据或获取API,
我试图在reactJs中从nodeJs获取数据,但得到的响应为空。
我的代码在下面。
Node.js:
router.get('/auth', function(req, res, next) {
if (req.query.code) {
let url = 'https://zoom.us/oauth/token?grant_type=authorization_code&code=' + req.query.code + '&redirect_uri=' + process.env.redirectURL;
request.post(url, (error, response, body) => {
// Parse response to JSON
body = JSON.parse(body);
console.log(`access_token: ${body.access_token}`);
console.log(`refresh_token: ${body.refresh_token}`);
if (body.access_token) {
request.get('https://api.zoom.us/v2/users/me', (error, response, body) => {
if (error) {
console.log('API Response Error: ', error)
} else {
body = JSON.parse(body);
console.log('API call ', body);
var JSONResponse = '<pre><code>' + JSON.stringify(body, null, 2) + '</code></pre>'
res.send(`
<style>
</style>
<h2>${body.first_name} ${body.last_name}</h2>
<p>${body.role_name}, ${body.company}</p>
${JSONResponse}
`);
}
}).auth(null, null, true, body.access_token);
} else {
// Handle errors, something's gone wrong!
}
}).auth(process.env.clientID, process.env.clientSecret);
return;
}
React.js:
componentDidMount() {
const url = 'http://localhost:3000/api';
axios.post(
url,
{
path: '/v2/users/info@data.nyc',
methode: 'GET'
}
)
.then((response) => {
console.log(response)
},
(error) => {
console.log(error)
}
);
}
您的axios调用不正确。请在下面尝试。
componentDidMount() {
const url = 'http://localhost:3000/api';
axios
.get(url + '/v2/users/info@data.nyc')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
});
}
我建议你修复axios页面:
试试这个:
const url = 'http://localhost:3000/api';
const zoom = () => {
Axios.get(url + '/v2/users/info@data.nyc')
.then((response) => {
console.log(response)
})
};