如何将HTML响应从操作返回到组件

时间:2019-05-22 19:13:22

标签: reactjs redux react-redux redux-thunk

I have a GET url which returns HTML data as its response. I am able to see the html response in networks tab in chrome but the same is not returned to my component. 

Adding code snippet for my implementation.

组件代码:

import { getResult } from 'REDUX/actions/appConfigActions';

componentDidMount() {
  const { actions: { getResult }} = this.props;
  getResult();
}

render() {
const { searchResult } = this.props;
console.log('searchResult  >>', searchResult);
return (
   <div>{searchResult}</div>
)
}
};

const mapStateToProps = state => ({
 searchResult: state.appConfig.searchResult,
});

const mapDispatchToProps = dispatch => ({
 actions: bindActionCreators({ getResult }, dispatch),
});

操作代码:  我尝试设置标头,但调用该操作仍然无效。我们正在使用 superagent-defaults 进行请求。我一直在参考SuperAgent link来实现。

export const getResult = () => {
const _url = 'someworkingurl';
  return (dispatch) => {
    req.get(`${_url}`)
       .type('text/html;charset=utf-8')
       .set('content-type', 'text/html;charset=utf-8')
       .set('Accept', 'text/html')
      .set('Content-Encoding', 'gzip')
      .accept('text/html;charset=utf-8')
     .then(({ body  }) => dispatch({
          type: 'result',
          payload: body
        }))
        .catch(err => dispatch(getError));
  };
};

0 个答案:

没有答案