【问题标题】:axios in response.headers doesn't show the x-operationresponse.headers 中的 axios 不显示 x 操作
【发布时间】:2018-05-06 13:31:07
【问题描述】:

我的 API 响应有问题。我们使用作为后端服务来注册异步用户。

我想检查用户是否已注册,但我没有简单的消息作为响应,但我需要检查标题:

x-操作:操作/e18f9239-b66b-4a81-a7cb-622a40326057 如果用户已使用此电子邮件注册,此标头将告诉我是否将 api 请求发送到另一个端点。

在网络部分,我有以下响应标头:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:8080
Content-Length:0
Date:Wed, 22 Nov 2017 17:33:20 GMT
Server:Kestrel
Vary:Origin
x-operation:operations/e18f9239-b66b-4a81-a7cb-622a40326057
x-resource:account

根据我的要求,我正在使用 axios 库(react-app)

export function signupUser({ email, password }) {
  return function(dispatch) {
    axios.post(`${API_URL}/sign-up`, { email, password })
    .then(response => {
      console.log('response', response)
      // browserHistory.push('/signin');
    })
    .catch((error) => {
      // dispatch(authError(error.data.error))
      console.log('error', error)
    })
  }
}

问题在于处理响应。我需要检查 x 操作,但我的响应中的 x 操作没有出现。

当我在 .then 中执行 console.log(response) 时,我的 headers: {} 为空。 这是console.log(response)

response 
{data: "", status: 202, statusText: "Accepted", headers: {…}, config: {…}, …}
config:
{transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", adapter: ƒ, …}
data:""
headers:{}
request:XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …}
status:202
statusText:"Accepted"

如何查看响应头中的x-操作,如果我在axios响应中没有,只出现在网络部分?

【问题讨论】:

    标签: javascript reactjs xmlhttprequest axios


    【解决方案1】:

    试着看看这个 SO 帖子:Axios get access to response header fields

    您需要确保您的服务器配置为允许客户端访问这些标头,例如:Access-Control-Expose-Headers: x-operation 我相信。

    【讨论】:

    • 是的,我在这里新建一个后发现了这个主题。我向后端服务询问了这个问题。当他们回复我时会提出解决方案。谢谢
    猜你喜欢
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多