不能与自定义首部和数据发送POST请求中反应天然

问题描述 投票:0回答:2

我想POST请求发送到服务器的自定义页眉和数据,但服务器在Android临危空对象。我使用的是爱可信和0.18.0反应本土0.57.8

const Feedback  = () => {
    return dispatch => {
        dispatch(fetchingData(true));
        axios.post(url, {x: 'x'}).then(res => {
            dispatch(fetchDataSuccess(true));
            dispatch(feedback(res.data));
        }).catch(err => {
            dispatch(fetchDataFailure(err));
        });
}

和标题为Axios公司:

axios.defaults.headers.common['Authorization'] ="Bearer " + e;

我测试,这不是一个服务器的问题。我也试图获取发送邮件。

android reactjs react-native
2个回答
0
投票

您可以添加配置的头,并将其添加内部反馈

const config = {
            headers: {
                'content-type': 'multipart/form-data',
                'Authorization': 'Bearer '+token
            }
        };
    const Feedback  = () => {
        return dispatch => {
            dispatch(fetchingData(true));
            axios.post(url, config).then(res => {
                dispatch(fetchDataSuccess(true));
                dispatch(feedback(res.data));
            }).catch(err => {
                dispatch(fetchDataFailure(err));
            });
    }

0
投票

您需要将头作为第三个参数的API调用,

const Feedback = () => {
  return dispatch => {
    dispatch(fetchingData(true));
    axios.post(
        url,
        { x: "x" },
        {
          headers: {
            authorization: "your authorization"
          }
        }
      )
      .then(res => {
        dispatch(fetchDataSuccess(true));
        dispatch(feedback(res.data));
      })
      .catch(err => {
        dispatch(fetchDataFailure(err));
      });
  };
};
© www.soinside.com 2019 - 2024. All rights reserved.