【发布时间】:2018-01-09 18:19:50
【问题描述】:
我正在尝试使用 axios get 请求将数据从我们的 TeamCity 服务器返回到我的 React Web 应用程序。不幸的是,我无法在服务器上启用 CORS,并且总是以 401:未授权或“预检响应无效(重定向)”错误结束。
我已尝试在标题中传递用户名和密码:
componentDidMount() {
var config = {
"headers": {
"username": "username",
"password": "password"
}
}
// Make axios http request to TeamCity server with authentication headers
axios.get('http://teamcity/app/rest', config)
.then(res => res.json())
.then(res => {
// Transform the raw data by extracting the nested posts
const projects = res.data.projects;
// Update state to trigger a re-render.
// Clear any errors, and turn off the loading indicator.
this.setState({
projects,
loading: false,
error: null
});
})
.catch(err => {
// Something went wrong. Save the error in state and re-render.
this.setState({
loading: false,
error: err
});
});
}
我已尝试按照此帖子在 url 中传递用户名和密码: How to pass username and password in TeamCity REST API
http://user:pass@server/app/rest/
我还尝试通过工作邮递员请求传递基本身份验证标头。
请求总是返回'XMLHttpRequest 无法加载http://teamcity/app/rest。预检响应无效(重定向)'
我与 TeamCity 服务器在同一个网络上。如果不启用 CORS,这可能吗?
【问题讨论】:
-
这似乎是一个身份验证问题。您没有按预期传递用户/通行证。
标签: rest reactjs cors teamcity axios