简体   繁体   English

Axios GET请求无效

[英]Axios GET request not working

I'm using: Axios: 0.17.1 Node: 8.0.0 我正在使用:Axios:0.17.1节点:8.0.0

The following standard Node get works fine, but the Axios version does not. 以下标准Node可以正常工作,但Axios版本没有。 Any ideas why? 有什么想法吗?

Node http: 节点http:

    http
    .get(`${someUrl}`, response => {
        buildResponse(response).then(results => res.send(results));
    })
    .on('error', e => {
        console.error(`Got error: ${e.message}`);
    });

Axios: 爱可信:

axios
    .get(`${someUrl}`)
    .then(function(response) {
        buildResponse(response).then(results => res.send(results));
    })
    .catch(function(error) {
        handleError(error, res);
    });

I just get a 503 in the catch, with "Request failed with status code 503" 我只是抓到了503,“请求失败,状态码为503”

It seems that you can pass Proxy details to Axios FYI. 您似乎可以将代理详细信息传递给Axios FYI。

From the docs... 来自文档......

  // 'proxy' defines the hostname and port of the proxy server
  // Use `false` to disable proxies, ignoring environment variables.
  // `auth` indicates that HTTP Basic auth should be used to connect to the proxy, and
  // supplies credentials.
  // This will set an `Proxy-Authorization` header, overwriting any existing
  // `Proxy-Authorization` custom headers you have set using `headers`.
  proxy: {
    host: '127.0.0.1',
    port: 9000,
    auth: {
      username: 'mikeymike',
      password: 'rapunz3l'
    }
  },

The only thing that worked for me was unsetting the proxy: 唯一对我有用的是取消设置代理:

delete process.env['http_proxy'];
delete process.env['HTTP_PROXY'];
delete process.env['https_proxy'];
delete process.env['HTTPS_PROXY'];

From: Socket hang up when using axios.get, but not when using https.get From: Socket在使用axios.get时挂断,但在使用https.get时则不挂起

I think you might forgot to return axios response. 我想你可能忘了回复axios的回应。

return axios
    .get(`${someUrl}`)
    .then(function(response) {
        return buildResponse(response).then(results => res.send(results));
    })
    .catch(function(error) {
        handleError(error, res);
    });

Notice return before axios.get and before buildResponse 注意在axios.get之前和buildResponse之前返回

use withCredentials property in your request config which will resolve your issue. 在您的request config使用withCredentials属性,这将解决您的问题。

 axios
    .get(`${someUrl}`, { withCredentials: true })
    .then(function(response) {
        return buildResponse(response).then(results => res.send(results));
    })
    .catch(function(error) {
        handleError(error, res);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM