如何在 axiuos 中的 reacj js 中获取 nodejs api 数据或 fetch api

问题描述

我有一个关于缩放 API 的问题。 如何在 axiuos 或 fetch API 中获取 reacjJs 中的 nodeJs 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',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,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)
  }) 
};

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...