javascript Axios GET 请求不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47437176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Axios GET request not working
提问by Ben Taliadoros
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. Any ideas why?
下面的标准 Node get 工作正常,但 Axios 版本不行。任何想法为什么?
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”
回答by Zinc
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'
}
},
回答by Ben Taliadoros
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
回答by Amit Kumar Khare
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 之前返回
回答by Roopak PutheVeettil
use withCredentialsproperty in your request configwhich will resolve your issue.
使用withCredentials您的财产,request config这将解决您的问题。
axios
.get(`${someUrl}`, { withCredentials: true })
.then(function(response) {
return buildResponse(response).then(results => res.send(results));
})
.catch(function(error) {
handleError(error, res);
});

